The function strcmp () is widely used in sorting of lists of names. Let S1 and S2 be the names of two strings. The function compares lexicographically (dictionary style) string S1 with string S2. It returns -1, 0, 1, respectively, if S1 is less than S2 , equal to S2, or greater than S2. Program illustrates its application.
#include<conio.h>
#include<iostream.h>
#include<string.h>
void main()
{
char word1 [20];
char word2[20];
clrscr();
cout<<"Enter First Word : ";
cin>>word1 ;
cout<<"Enter Second Word : ";
cin>>word2;
if ( strcmp (word1,word2) == 0)
cout<<"Identical Words";
else
cout<<((strcmp(word1, word2) > 0) ? word2 : word1)<< " Comes Before "<<((strcmp(word1, word2) < 0) ? word2 : word1);
getch();
}