Standard Library String Handling Functions:


There are a large number of library functions available in C that allows to operate strings. Using these operations, the strings can be compared, copied and concatenated (i.e. combine one after another). Some of commonly available library functions in C are listed below. They all are included in a header file string.h.

1.     strlen ( ): This function returns the number of characters in a string, not counting the terminating NULL character. For example:
int a;
                                    char name[ ]= “ABC”;
                                    a = strlen (name);
will store 3 in a as the length of the string.

2.      strcpy ( ): The strcpy function works 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, the statement
                                    strcpy (city,”DELHI”);
            will assign the string “DELHI” to the variable city. Similarly the statement
                                    strcpy (city1,city2);
will assign the contents of the string variable city2 to the variable city1. The size of the array city1 should be large enough to receive the contents of city2..


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


string1 and string2 are character arrays. When the function strcat is executed, string2 is appended to string1. It does so by removing the null character at the string1 and placing string2 from there. The string at string2 remains unchanged. For example, consider the following strings:




4.      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:




string1 and string2 may be string variables or string constants. Examples are:
                                    strcmp (name1, name2);
                                    strcmp (name1, “JHON”);
                                    strcmp (“ROM”, “RAM”);
Our major concern is to determine wheather the strings are equal; if not, which is alphabetically above. The mismatch is rarely important. For example, the statement
                                    strcmp (“their”, “there”);

will return a value of -9 which is the numerical difference between ASCII “i” and ASCII “r”. That is “i” minus “r” in ASCII code is -9. If the value is negative, string1 is alphabetically above string2.


5.      strstr ( ) function:
It is a two- parameter function that can be used to locate s sub-string in a string. This takes the forms:
                                    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, For example:
                                    if (strstr (s1,s2 == NULL)
                                                printf(“substring is not found”);
                                    else
                                                printf(“s2 is a sub- string of s1);



Previous                                                                                    Next


Powered by Blogger.