by Dinesh Thakur Category: Functions

 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.


void main()
                char word1 [20];
                char word2[20];
                cout<<"Enter First Word : ";
                cin>>word1 ;
                cout<<"Enter Second Word : ";
         if ( strcmp (word1,word2) == 0)
                          cout<<"Identical Words";
                cout<<((strcmp(word1, word2) > 0) ? word2 : word1)<< " Comes Before "<<((strcmp(word1, word2) < 0) ? word2 : word1);

Compare Strings: Strcmp

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles