Tuesday, June 8, 2010

*Handling of Character String-2

Handling of Character Strings - 2

We would start here, with the arithmetic operations on characters and strings. C allows us to manipulate characters the same way as we do with numbers. Whenever a character constant or character variable is used in an expression, it is automatically converted into an integer value by the system. The integer value depends on the local character set of the system.

For example, if the machine uses ASCII representation, then,

m = 'a';

printf("%d\n",m);

will display the number 97 on the screen.

Arithmetic operations can also be performed on the character constants and variables.

For example,

s = 'z' - 1;

will cause 1 to be subtracted from the ASCII value of 'z'(122) and the resultant value of 121 to be assigned to s.

The C library supports functions for converting strings into their corresponding integer values.

The function is of the form

d = atoi(string);

Putting strings together

We cannot apply the arithmetic addition for joining of two or more strings in the manner

string1 = string2 + string3; or

string1 = string2 + "SACY";

For carrying out the above we need to write a program to copy the contents of the string2 & string3 into string1 one after the other. This process is called concatenation of strings.

We now see some functions provided by the C library to overcome the above problem & also provide additional features.

strcat() Function

strcat() joins two or more strings together. It takes the following form

strcat(string1, string2);

string1 and string2 are character arrays. When the above function is executed, string2 is appended to string1. It does so by removing the null character at the end of string1 and placing string2 from there. The string at string2 remains unchanged.

strcat function may also append a string constant to a string variable. The following is valid

strcat(part1,"SACY");

C also permits nesting of strcat functions. For example

strcat(strcat((string1,string2),string3);

is allowed and concatenates all the three strings together. The resultant string is stored in string1.

strcmp() Function

The strcmp function compares two strings identified by the arguments and has a value 0 if they are equal. If they are not, it has the numeric difference between the first non-matching characters in the strings. It takes the form:

strcmp(string1, string2);

string1 and string2 may be string variables or string constants. For example

strcmp(name1,name2);

strcmp(name1,"JOHN");

strcpy() Function

The strcpy function works almost like a string-assignment operator. It takes the form

strcpy(string1,string2);

and assigns the contents of string2 to string1. string2 may be a character array variable or a string constant. For example

strcpy(city,"DELHI");

will assign the string "DELHI" to the string variable city. Similarly the statement

strcpy(city1, city2);

will assign the contents of the string variable city2 to the string variable city1.The size of the array city1 should be large enough to receive the contents of city2.

strlen() Function

This function counts and returns the number of characters in a string

n = strlen(string);

Where n is an integer variable, which receives the value of the length of the string. The argument may be a string constant. The counting ends at the first null character.

– 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