Society & Lifestyle
|Computing||Share This Page|
Handling of Character Strings - 2
|by Sachin Mehta|
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';
will display the number 97 on the screen.
Arithmetic operations can also be performed on the character constants and variables.
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() joins two or more strings together. It takes the following form
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
C also permits nesting of strcat functions. For example
is allowed and concatenates all the three strings together. The resultant string is stored in string1.
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:
string1 and string2 may be string variables or string constants. For example
The strcpy function works almost like a string-assignment operator. It takes the form
and assigns the contents of string2 to string1. string2 may be a character array variable or a string constant. For example
will assign the string "DELHI" to the string variable city. Similarly the statement
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.
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.
|More by : Sachin Mehta|
|Views: 3378 Comments: 0|
|Top | Computing|