Strcmp :
strcmp(str1,str2) compares str1 and str2 lexicographically .Returns a negative value if str1<str2; 0 if str1 and str2 are identical; and positive value if str1>str2.
Syntax :
int strcmp( char *str1, char *str2 );
C – STRCMP EXAMPLE PROGRAM :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include<stdio.h> #include<conio.h> #include<string.h> int main() { char *str1,*str2; int i; clrscr(); printf(“Enter first string \n”); gets(str1); printf(“Enter second string \n”); gets(str2); printf(“str1 :%s \n str2 :%s\n “,str1,str2); i=strcmp(str1,str2); if(i==0) printf(“str1 and str2 are identical \n”); else if(i<0) printf(“str1<str2\n”); else printf(“str1>str2\n”); getch() return 0; } |
Output :
Enter first string
I love the world Enter second string I love the world str1 : I love the world str2 : I love the world str1 and str2 are identical |