Search This Blog

String Comparison in C and Working of strcmp function

In both C and C++ programming languages, an inbuilt function strcmp() is used to compare two strings. This function is defined in string.h header file. So, to use this function, you have to include the header file string.h in the program. Most of the people ( i mean beginners) have a wrong idea about the working of the this function. I also had made some false assumptions. This function takes two strings as arguments. And it compares the two strings using the ASCII equivalent of each character. Some of the false ideas about this function are:
  • The function returns 1 when the strings are equal and 0 when they are not equal
  • The function returns 1 when they are not equal
  • The function returns 1 when the first string is greater than the second and returns -1 when the first one is less than the second one
The correct statements about this function are:
  • The function returns 0 when the strings are equal
  • The function returns a positive value (which depends on the strings) when the first string is greater than the second
  • The function returns a negative value (which depends on the strings) when the first string is less than the second

And the 'less than' and 'greater than' are not based on the length of the string. As i have told earlier, although string is array of characters, each individual character is considered like an integer. And this integer value can be understood from the ASCII chart. To see the ASCII chart, click the following link.

ASCII chart

The comparison is as follows:

Both the strings are considered character by character. First, the function takes the first character of both and subtracts the ASCII equivalent of the character from second string from that of the character from the first string. If the difference is positive or negative, the comparison is over and this difference is returned by the function. If the first letters of both are the same, the difference will be zero. Then it takes the next character of both the strings and compares until the end (occurrence of null character) of either of the strings or getting a non zero difference. When a non zero difference is obtained , the function stops comparison and returns this value. If no non zero difference is obtained until the end of either of the strings, then it will return the value zero indicating that both are equal.

For example, let the input strings be "kings" and "kingfisher". Suppose we are using "kings" as first parameter and "kingfisher" as second.


strcmp("kings" , "kingfisher");



The function will compare these strings and give an output 13. The function will first take the first character of both. They are same (k) and therefore the difference is zero. So, it will look for the second. Again equal (i). Then it will look for third. equal. Then fourth. Again it is equal. Then looks for 5th. And now they are different ( s and f ). The function will return the difference s-f, i.e 115-102 which is equal to 13.



#include<string.h>
#include<stdio.h>&#62
#include<conio.h>
void main()
{
char st1[80], st2[80];
int i;
printf("Enter the first string: ");
gets(st1);
printf("Enter the second string: ");
gets(st2);
printf("The size of %s is %d:\n", st1, strlen(st1));
printf("The size of %s is %d:\n", st2, strlen(st2));
i = strcmp(st1,st2);
if (!i)
     printf("The strings are equal.\n");
else if (i<0)
printf("\nstring %s is greater",st2);
else
printf("\nstring %s is greater",st1);
getch();
}

No comments: