Write about String Handling Functions in C language?

The C library supports a large number of strings handling functions that can be used to carry out many of the string manipulations.

The most commonly used string functions:

1. strcat ( ) – concatenates two strings.

2. strcmp() – compare two strings.

3. strcpy ( ) – copies one string over another.

4. strlen ( ) –finds the length of a string.

strcat ( ):

The strcat function joins two strings together.It takes the following form:

strcat ( string1, string2);

str1 and str2 are character arrays. When the function strcat is executed ,str2 is appended to str1. It doesn’t removes the null character at the end of str1 and placing str2 from there.The string at str2 remains unchanged.

Example :




0
1
2
3
4
5
6
STR 1=
V
E
R
Y
\0


STR 2=
G
O
O
D
\0



strcat ( str1, str2);

strings


We must move sure that the size of str1 is large enough to accommodate the final string. strcat function may also append a string constant to a string variable.

starcat ( str1, “Good);

C permits nesting of strcat functions:

strcat ( strcat ( str1, str1) str3);

It concatenates all the 3 strings together. The resulting string is stored in str 1.

strcmp ( ):

The strcmp function compares two strings identified by the arguments and have a value 0 if they are equal. If they are not equal, it has the numeric difference between. The first nonmatching characters in the strings.

strcmp ( str1, str2);

str1 and str2 may be string variables or string constants.

strcmp ( name1, name2);

strcamp ( name1, “John”);

strcmp ( "ROM”, “Ram”);

Ex: strcmp ( "their “, “there”);
       ASCII (i) –ASCII ( r)
      105 – 114 =  -9

strcpy ( ):

The strcpy function works almost like a string assignment operator.

strcpy ( str1, str2);

Assigns the contents of str2 to str1 may be a character away variable or character constant.

strcpy ( city, “”DELHI”);

Will assign the string DELHI to the string var: city. The size of the away city should be large enough to receive the string constant.

strcpy ( city1, city2);

Assign contents of string var city2 to the string variable city1.

strlen ( ):

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

n = strlen ( string);

where n is an integer variables , which receiver the value of the length of the string. The argument may be string constant. The counting ends at the first null character.

OTHER STRING FUNCTIONS:

The header file < strigh.h > contains many more string manipulation functions.

strncpy():

The function strncpy that copies only the left –most n characters of the source string to the target string variable. This is a three-parameter function and is invoked as:

strncpy (s1,s2,5);

This statement copies the first 5 characters of the source string size s2 into the target string s1.

strncmp():

This function has three parameters as shown below.

strncmp ( s1, s2, n);

This compares the left most n characters of s1 to s2 and returns:

a) 0 of they are equal.

b) –ve number, if s1 sub-string is less than s2 and

c) +ve number , otherwise.

strncat():

This is another concatenation function that takes three parameters as:

strncat ( s1, s2, n);

This will concatenate the left most n characters of s2 to the end of s1.

Example:

strings1


After strncat ( S1, S2,4); execution:

s1=

B
A
L
A
G
U
R
U
\0

strstr():

It is a two-parameter function that can be used to locate a sub-string in a string.

strstr (s1, s2);

strstr (s1, “ABC”);

The function strstr searches the string s1 to see whether the string s2 is contained in s1. If yes the function returns the position of the first occurrence of the sub-string. Otherwise, it returns a NULL pointer.

Ex: 

if(strstr (s1,s2) = = NULL)
printf (“substring is not found”);
else
printf ( “S2 is a substring of S1);

We also have functions to determine the existence of a character in a string.

strchr ( s1, ‘m’);

Will locate the first occurrence of the character ‘m’.

strrchr (s1, ‘m’);

Will locate the last occurrence of the character ‘m’ in the string s1.

strrev():

It is used to reverse the given string.

strrev ( “Hai”);     
  
Output: iaH.

strupr ( ):

This is the function used to convert the given string to upper case letters.

strupr (“Sasi”);  
  
Output: SASI

strlwr ( ):

This is the function used to convert the given string to lower case letters.

strlwr ( “SASI’);     

Output: sasi

Related

C Language 5600710531212677150

Post a Comment

emo-but-icon

item