by Dinesh Thakur Category: Puppetting On Strings

A typical call to the strcmp function take the following form:

strcmp( s1, s2)

This function performs a case-sensitive comparison between strings s1 and s2 and returns an integer value as follows: negative number if s1 < s2, zero if s1 == s2 and a positive number if s1 > s2. The return value indicates the lexicographic ordering of strings.

The comparison starts with the first character in each string and continues with subsequent characters until the corresponding characters differ or the end of either or both the strings is reached. During comparison, if a character in one string is smaller, that string is considered as the smaller string and the appropriate value is returned. However, if the characters being compared are equal, the comparison proceeds for the next character. Now, if the end of a string is encountered, that string is considered to be lexicographically smaller than the other string. However, if all characters in both strings are exhausted, with all previous characters being equal, the strings are considered to be lexicographically equal. This is illustrated below with the help of several examples.

First consider the comparison of strings "Hello" and "World". Initially, the characters 'H' and ‘w’ are compared. Since 'H' < 'W', string "Hello" is considered to be lexicographically smaller and the function returns a negative value. Note that the subsequent characters in strings are not compared in this case.

Next consider the comparison of strings "He1p" and "Hello". As the first three characters in these strings are equal on comparison, the fourth character pair, 'p' and ' 1', is compared. Since 'p' > ‘1’, the first string is lexicographically larger than the second string and the function returns a positive value.

Now consider the comparison of strings "Hell" and "Hello". The first four characters compare equal after which the characters in the first string are exhausted. Thus, string "Hell" is considered to be smaller and the function returns a negative value.

Finally, consider the comparison of two equal strings: "Hello" and "Hello”. In this case, all the characters compare equal. After the comparison of letter 'o' in each string, the characters in both strings are exhausted. Thus, the strings are considered to be equal and the function returns a zero value.

The program segment given below reads two strings from the keyboard and compares them

#include <stdio.h>

void main()
{
      char str1[20], str2[20];
      int res;
      clrscr();
      printf("Enter two strings: ");
      scanf("%s %s", str1, str2);
      res= strcmp(str1, str2);
      if (res < 0)
          printf("String \"%s\" is smaller than \"%s\"\n", str1, str2);
      else if (res > 0)
          printf("String \"%s\" is greater than \"%s\"\n", str1, str2);
      else printf("String \"%s\" is smaller than \"%s\"\n", str1, str2);
         getch();
}

The strings are accepted in character arrays strl and str2. Then the strcmp function is used to Compare them (case sensitive comparison)and store the return value in variable res. The value of res is then tested using an if-else-if statement and an appropriate message string is printed. The output of the program containing this code segment is shown below.

            String comparison



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), 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.