Home | Hindi | Kabir | Poetry | Workshop | BoloKids | Writers | Contribute | Search | Contact | Share This Page!                      Shop Online

  News
Channels
In Focus

Analysis  
Bolography  
Cartoons
Environment   
Opinion 

Columns
 Business
 My Word 
 PlainSpeak 
 Random Thoughts 
Our Heritage

Architecture
Astrology
Ayurveda
Buddhism
Cinema 
Culture
Dances
Festivals
Hinduism
History  
People  
Places 
Sikhism
Spirituality 
Vastu 
Vithika  

Society & Lifestyle

Family Matters 
Health
Parenting
Perspective 
Recipes
Society
Teens 
Women 

Creative Writings

Book Reviews
Ghalib's Corner
Humor
Individuality
Jagoji
Literary Shelf 
Love Letters  
Memoirs
Musings
Ramblings
Stories
Travelogues

Computing
  General Articles
 
CC++ 
  Flash 
  Internet Security 
 
Java 
 
Linux     
  Networking  

Handling of Character Strings

A string is  a collection of characters. In terms of programming, it can be defined as an array of characters. Any group of characters (except double quote sign) defined between double quote marks is a constant string. For example, if the following string 

"Sachin is a good programmer."  

has to be displayed on the screen, then we will give  

printf("\"Sachin is a good programmer.\"");  

in the program. The \" is to print the double quote itself onto the screen.

The common operations related to character strings are:  

  • Reading and writing strings  

  • Combining strings together

  • Copying one string to another

  • Comparing strings for equality

  • Extracting a portion of a string

  • Declaring and initializing string variables  

String variables are declared similar to any other array variables in C. For example if we want to declare a string variable str of  a maximum size of 8 characters, then it can be done as follows  

char str[8];  

When the compiler assigns a character string to a character array, it automatically supplies a null character ('\0') at the end of the string. So the size of string should be such that it can hold the entire string plus one null character.

In order to avoid the confusion we can give initializations without specifying the array size. For example:

char str[] = {'S','A','C','Y','\0'}

This will define a string of five elements.

Reading strings from terminal 

The function scanf with the %s format allows us to read a word input by the user, the same way as integers , real values & characters  are read using %d, %f & %c respectively.

For example

char name[10];  
scanf("%s",name);

Dissimilar to the integer , float & characters the %s format doesnt require the ampersand before the variable name. The main problem is, that the scanf  stops reading a string of characters when it encounters the first white space. So if we give for above example

Sachin Mehta

Then the scanf statement will read only Sachin into the variable name.

In many text processing applications, we need to read an entire line of text. In that case as we can't use scanf statement, we can always use the getchar funtion to read the entire line or word as follows

char line[80],ch;  
int c = 0;  
do  
{  
ch = getchar();  
line[c] = ch;  
c++;  
}while(ch != '\n');  
c = c-1;  
line[c] = '\0';

Writing strings to screen

We have seen how we can use printf statement to directly output the string within double quotes to the screen. Now we will see the outputting of strings using string variables. For example the contents of the variable name can be printed as

printf("%s",name);

We also have something called the precision with which  the string variable has to printed out.

For example         printf("%7.3s",name)

This specifies that only the first 3 characters have to be printed in a total field width of 7 characters &  right justified in the allocated width by default. We can include a minus sign to make it left justified(%-7.3).

The following points should be noted  

  1. When the field width is less than the length of the string, the entire string is printed.  

  2. The integer value on the right side of the decimal point specifies the number of characters to be printed.  

  3. When the number of characters to be printed is specified as zero, nothing is printed.  

  4. The minus sign in the specification causes the string to be printed left-justified.  

We will see  rest of the topics related to strings in the next article.     

– Sachin Mehta
March 30, 2001

C/C ++        
Managing Input-Output Operations
     
Managing Output Operations
    
Decision Making And Branching
     
Decision Making and Branching (if statement)
     
Decision Making and Looping
    
The Do Statement
   
Arrays
    
Arrays - Multi-Dimensional
    
Handling of Character Strings
    
Handling of Character Strings - 2  
User Defined Functions   
   User Defined Functions...contd    
Handling of Functions  

Computing
Flash | Internet Security 
Java | Linux | Networking  

General Articles  

Top | Previous | Next 
 


 

Recommend This Page!

Analysis | Architecture | Astrology | Ayurveda | Book Reviews | Buddhism | Cartoons | Cinema | Computing | Culture | Dances
Environment | Fables | Family Matters | Festivals | Hinduism | Health | History | Home Remedies | Humor | Individuality | Jagoji
Literary Shelf | Memoirs | Musings | Opinion | Parenting | Perspective | Photo Essays | Places | Ramblings
Random Thoughts | Recipes | Sikhism | Society | Spirituality | Stories | Teens | Travelogues | Vastu | Vithika | Women

Home | Hindi | Bolography | BoloKids | Kabir | Poetry | Quotes | Workshop | Writers | Contribute | Search | Contact


Boloji.com is owned and managed by Boloji Media Inc

Privacy Policy | Disclaimer
No part of this Internet site may be reproduced without prior written permission of the copyright holder.