String library function in C Programming

String library function 

There are several string library functions used to manipulate string and the prototypes for these functions are in header file “string.h”. Several string functions are 


This function return the length of the string. i.e. the number of characters in the string excluding the terminating NULL character. 

It accepts a single argument which is pointer to the first character of the string. 

For example- 


    It return the value 6. 

In array version to calculate length:- 

        int str(char str[]) 


                int i=0; 





                return i; 





        void main() 


                char str[50]; 

                print(”Enter a string:”);


                printf(“Length of the string is %d\n”,strlen(str)); 



    Enter a string: C in Depth 

    Length of the string is 8 


This function is used to compare two strings. If the two string match, strcmp() return a value 0 otherwise it return a non-zero value. It compare the strings character by character and the comparison stops when the end of the string is reached or the corresponding characters in the two string are not same.


            return a value: 

                    <0 when s1< s2

                    =0 when s1=s2

                    >0 when s1>s2 

The exact value returned in case of dissimilar strings is not defined. We only know that if s1s2 then a positive value will be returned. 

For example:

/*String comparison…………………….*/ 



void main() 

        char str1[10],str2[10]; 

        printf(“Enter two strings:”); 





                printf(“String are same\n”); 

         } else { 

                printf(“String are not same\n”); 



This function is used to copying one string to another string. The function strcpy(str1,str2) copies str2 to str1 including the NULL character. Here str2 is the source string and str1 is the destination string.

The old content of the destination string str1 are lost. The function returns a pointer to destination string str1. 




void main() 

        char str1[10],str2[10]; 

        printf(“Enter a string:”); 



        printf(“First string:%s\t\tSecond string:%s\n”,str1,str2); 



        printf(“First string :%s\t\tSecond string:%s”,str1,str2); 



This function is used to append a copy of a string at the end of the other string. If the first string is “”Purva” and second string is “Belmont” then after using this function the string becomes “PusvaBelmont”. The NULL character from str1 is moved and str2 is added at the end of str1. The 2nd string str2 remains unaffected. A pointer to the first string str1 is returned by the function. 




void main() 

        char str1[20],str[20]; 

        printf(“Enter two strings:”); 




        printf(“First string:%s\t second string:%s\n”,str1,str2); 


        printf(“Now first string is %s\n”,str1); 


Enter two strings: data Base

First string: database second string: database  

Now first string is: database-one


"string library in c"

"string library function in c"

"string operations in c without using library functions"

"string library functions"

"string library function in c programming"

"string library functions in c with examples"

"string library functions in c++"

"what is library function in c with example"

"string operations in c without library functions"