Tuesday, June 8, 2010

*Handling of Character String

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

"Simi is a good programmer."

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

printf("\"Simi 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

Simi

Then the scanf statement will read only Simi 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.

Ravinder Singh
*ks*

No comments:

DESCLAIMER:

KIMONGSHAW (RAVINDER SINGH c/o Principal, GDC Chamba, Distt Chamba himachal pradesh 176311 india) ACKNOWLEDGES THAT THOUGH i TRY TO REPORT ACCURATELY, i CANNOT VERIFY THE ABSOLUTE FACTS OF EVERYTHING POSTED. POSTINGS MAY CONTAIN FACT, SPECULATION OR RUMOR. i FIND IMAGES FROM THE WEB THAT ARE BELIEVED TO BELONG IN THE PUBLIC DOMAIN. IF ANY STORIES OR IMAGES THAT APPEAR ON THE SITE ARE IN VIOLATION OF COPYRIGHT LAW, PLEASE EMAIL or call me AND i WILL REMOVE THE OFFENDING INFORMATION AS SOON AS POSSIBLE. contact me kimongshaw@yahoo.co.uk