Pages

Friday, September 30, 2016

Use of built-in C string library function.


// Use of built-in C string library function.

#include<stdio.h>
int main()
{
     int len;
     char str1[10], str2[10];
    
     puts("Enter String 1:");
     gets(str1);
    
     puts("Enter String 2:");
     gets(str2);
    
     //Function to find length of given string 1.
     len = strlen(str1);
     printf("\nLength of String 1 is %d.\n", len);
    
     //Function to compare two strings
     if(strcmp(str1,str2)==0)
           printf("\nString 1 and String 2 are same.\n");
     else
           printf("\nString 1 and String 2 are not same.\n");
          
     //Function to copy string 1 into string 2
     strcpy(str2,str1); //this will copy str1 into str2
     printf("\nString 2 after execution of strcpy = %s", str2);
    
     return 0;
}


Output of the program:

/*Output
Enter String 1:
abc
Enter String 2:
xyz

Length of String 1 is 3.

String 1 and String 2 are not same.

String 2 after execution of strcpy = abc
*/

No comments:

Post a Comment