Handling of Character Strings by Sachin Mehta SignUp
Boloji.com
Boloji
Home Kabir Poetry Blogs BoloKids Writers Contribute Search Contact Site Map Advertise RSS Login Register
Boloji
Channels

In Focus

Analysis
Cartoons
Education
Environment
Going Inner
Opinion
Photo Essays

Columns

A Bystander's Diary
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
Literary Shelf
Love Letters
Memoirs
Musings
Quotes
Ramblings
Stories
Travelogues
Workshop

Computing

CC++
Computing Articles
Flash
Internet Security
Java
Linux
Networking
Computing Articles Share This Page
Handling of Character Strings
by Sachin Mehta Bookmark and Share
 

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 ('') 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',''}

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 != ' ');  
c = c-1;  
line[c] = '';

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.    


30-Mar-2001
More by :  Sachin Mehta
 
Views: 2513
Article Comment Dear Sachin,
I have a difficulty in ā€˜Cā€™ programming, some of:
How can I access/link D Base database through ā€œCā€™.

Very thanks to you

Dhananjay
Dhananjay Pandey
09/30/2010
 
Top | Computing Articles







A Bystander's Diary Analysis Architecture Astrology Ayurveda Book Reviews
Buddhism Business Cartoons CC++ Cinema Computing Articles
Culture Dances Education Environment Family Matters Festivals
Flash Ghalib's Corner Going Inner Health Hinduism History
Humor Individuality Internet Security Java Linux Literary Shelf
Love Letters Memoirs Musings My Word Networking Opinion
Parenting People Perspective Photo Essays Places PlainSpeak
Quotes Ramblings Random Thoughts Recipes Sikhism Society
Spirituality Stories Teens Travelogues Vastu Vithika
Women Workshop
RSS Feed RSS Feed Home | Privacy Policy | Disclaimer | Site Map
No part of this Internet site may be reproduced without prior written permission of the copyright holder.
Developed and Programmed by ekant solutions