C Programming

String Comparison: C Programming

A string in C language is an array of characters, which is terminated with a null character (\0). Using this property strings are compared.

Two strings can be compared in various ways. In this tutorial, first, we will see a user-defined function to compare two strings, and then we will see some built-in library functions which can be used to compare two strings very easily. So, let’s get started.

String comparison using a user-defined function :

We will write a function stringCompare() to compare strings. We traverse the strings and compare each character of the string until we reach the end of any one or both or one mismatched are found. If the traversal is reached to the end of both the strings, then the strings are matched; otherwise, strings are mismatched.

01. /*userDefinedFunction.c*/  
02.  
03. #include<stdio.h>    
04.    
05. int stringCompare( char str1[ ], char str2[ ] )    
06. {    
07.     int i=0;    
08.    
09.     while( str1[i] == str2[i] )    
10.     {    
11.         if( str1[i] == '\0' || str2[i] == '\0' )    
12.             break;    
13.         i++;    
14.     }    
15.    
16.     if( str1[i] == '\0' &&  str2[i] == '\0' )    
17.         return 0;    
18.     else    
19.         return -1;    
20.    
21. }    
22.    
23.    
24. int  main()    
25. {    
26.     char str1[30],str2[30];    
27.    
28.     printf("Enter the first string: ");  
29.     scanf("%[^\n]%*c",str1);  
30.     printf("Enter the second string: ");  
31.     scanf("%[^\n]%*c",str2);  
32.  
33.     if(stringCompare(str1,str2) == 0)  
34.         printf("The strings are equal \n");    
35.     else  
36.         printf("The strings are not equal \n");    
37.    
38.     return 0;    39.    }

Here we traverse the strings using while loop and a variable i. When characters are equal in the same position of both strings, the value of i is incremented by 1 (line 13). If characters are not equal (line 09) or we reach the end of the string (line 11), then the while loop is a break. After the while loop, we check both the string traversals are reached to the end or not (line 16). If the traversal is reached to the end of both strings, then the strings are equal otherwise not.

String comparison using built-in library functions :

The following library functions can be used for string comparison. All the functions are declared in the string.h header file.

strcmp() function :

This function compares two strings passed to the function.

Syntax:

int strcmp(const char *str1, const char *str2)

Return value: Return 0 if the strings are equal. Return a negative integer if the ASCII value of the first unmatched character of the first string is less than the second string. Return a positive integer if the ASCII value of the first unmatched character of the first string is greater than the second string. Some systems return difference of the ASCII value of first mismatched character and some systems return -1 if the ASCII value of the first unmatched character of the first string is less than the second string and return 1 if the ASCII value of the first unmatched character of the first string is greater than the second string.

Example Return Value Explanation
strcmp( “Hello World”,”Hello World” ) 0 Two strings are the same.
strcmp( “Hello”,”Hello\0 World” ) 0 Strings are compared till the character ‘\0’. The first string by default ends with ‘\0’, and the second string contains the ‘\0’ character after ‘Hello’.
strcmp( “Hello\0\0\0″,”Hello\0 World” ) 0 Strings are compared till the character ‘\0’.
strcmp( “Hello World”,”hello World” ) Negative integer ASCII value of the first unmatched character of the first string (‘H’) is less than the second string (‘h’)
strcmp(“hello World”,”Hello World” ) Positive integer ASCII value of the first unmatched character of the first string (‘h’) is greater than the second string (‘H’)

strncmp() function :

This function is similar to the function strcmp(), but here we have to specify how many bytes are compared by passing an extra argument to the function.

Syntax:

int strncmp(const char *str1, const char *str2, size_t n)

Return value: The function returns 0 if the first n characters of the two strings are equal; otherwise, it returns negative or positive integer depending on the sign of the differences between the first mismatched character’s ASCII value.

Example Return Value Explanation
strncmp( “Hello World”,”Hello World”,5 ) 0 First 5 characters are the same.
strncmp( “Hello”,”Hello\0 World”,5 ) 0 First 5 characters are the same.
strncmp( “Hello\0\0\0″,”Hello\0 World”,8 ) 0 ‘\0’ is after the first 5 characters in both strings. So, comparison is stopped after 5 not 8.
strncmp( “Hello World”,”hello World”,5 ) Negative integer ASCII value of the first unmatched character of the first string (‘H’) is less than the second string (‘h’)

strcasecmp() function :

This function is similar to the function strcmp(), but here the strings are not case sensitive.

Syntax:

int strcasecmp(const char *str1, const char *str2)

Return value: Same as strcmp(), but strings are treated as case-in-sensitive.

Example Return Value Explanation
strcasecmp( “Hello World”,”Hello World” ) 0 Two strings are the same.
strcasecmp( “Hello”,”Hello\0 World” ) 0 Strings are compared till the character ‘\0’. The first string by default ends with ‘\0’, and the second string contain the ‘\0’ character after ‘Hello’.
strcasecmp( “Hello World”,”hello World” ) 0 Strings are case-in-sensitive. So, “Hello World” and “hello World” are the same.

strncasecmp() function :

This function is similar to the function strncmp(), but here the strings are not case sensitive.

Syntax:

int strncasecmp(const char *str1, const char *str2)

Return value: Same as strncmp(), when strings are treated as case-in-sensitive.

Example Return Value Explanation
strncasecmp( “Hello World”,”Hello World”,5 ) 0 First 5 characters are the same.
strncasecmp( “Hello”,”Hello\0 World”,5 ) 0 First 5 characters are the same.
strncasecmp( “Hello\0\0\0″,”Hello\0 World”,8 ) 0 ‘\0’ is after the first 5 characters in both strings. So, comparison is stopped after 5 not 8.
strncasecmp( “Hello World”,”hello World”,5 ) 0 Strings are case-in-sensitive. So, “Hello” and “hello” are the same.

memcmp() function :

This function compares two memory blocks byte by byte. We have to pass two pointers of the memory blocks and the number of bytes to compare.

Syntax:

int memcmp(const void *str1, const void *str2, size_t n)

Return value: The function returns 0 if the two memory blocks (n bytes) are equal; otherwise, it returns the differences between the first mismatched pair of bytes (bytes are interpreted as unsigned char objects, then promoted to int).

Example Return Value Explanation
memcmp( “Hello World”,”Hello World”,5 ) 0 First 5 characters are the same.
memcmp( “Hello\0\0\0″,”Hello\0 World”,8 ) Negative integer The first 6 characters are the same, but the 7th character is different. Here comparison not stopped like strncmp() when getting ‘\0’ character.
memcmp( “Hello World”,”hello World”,11 ) Negative integer ASCII value of the first unmatched character of the first string (‘H’) is less than the second string (‘h’)

Example:

Following is the C code example of all the functions discussed.

01. /*stringCompare.c*/    
02.  
03. #include<stdio.h>    
04. #include<string.h>    
05.  
06. int main()    
07. {    
08. printf("strcmp( "Hello World","Hello World" ) => %d\n",strcmp( "Hello World","Hello World" ));    
09. printf("strcmp( "Hello","Hello\\0 World" ) => %d\n",strcmp( "Hello","Hello\0 World" ));    
10. printf("strcmp( "Hello World","hello World" ) => %d\n",strcmp( "Hello World","hello World" ) );    
11. printf("strcmp( "Hello\\0\\0\\0","Hello\\0 World" ) => %d\n",strcmp( "Hello\0\0\0","Hello\0 World" ));    
12.  
13. printf("\n---------------\n");    
14.  
15. printf("strncmp( "Hello World","Hello World",5 ) => %d\n",strncmp( "Hello World","Hello World",5 ));    
16. printf("strncmp( "Hello","Hello\\0 World",5 ) => %d\n",strncmp( "Hello","Hello\0 World",5 ));    
17. printf("strncmp( "Hello\\0\\0\\0","Hello\\0 World",8 ) => %d\n",strncmp( "Hello\0\0\0","Hello\0 World",8 ));    
18. printf("strncmp( "Hello World","hello World",5 ) => %d\n",strncmp( "Hello World","hello World",5 ));    
19.  
20. printf("\n---------------\n");    
21.  
22. printf("strcasecmp( "Hello World","Hello World" ) => %d\n",strcasecmp( "Hello World","Hello World" ));    
23. printf("strcasecmp( "Hello","Hello\\0 World" ) => %d\n",strcasecmp( "Hello","Hello\0 World" ));    
24. printf("strcasecmp( "Hello World","hello World" ) => %d\n",strcasecmp( "Hello World","hello World" ));    
25.  
26. printf("\n---------------\n");    
27.  
28. printf("strncasecmp( "Hello World","Hello World",5 ) => %d\n",strncasecmp( "Hello World","Hello World",5 ) );    
29. printf("strncasecmp( "Hello","Hello\\0 World",5 ) => %d\n",strncasecmp( "Hello","Hello\0 World",5 ));    
30. printf("strncasecmp( "Hello\\0\\0\\0","Hello\\0 World",8 ) => %d\n",strncasecmp( "Hello\0\0\0","Hello\0 World",8 ));    
31. printf("strncasecmp( "Hello World","hello World",5 ) => %d\n",strncasecmp( "Hello World","hello World",5 ));    
32.  
33. printf("\n---------------\n");    
34.  
35. printf("memcmp( "Hello World","Hello World",5 ) => %d\n",memcmp( "Hello World","Hello World",5 ) );    
36. printf("memcmp( "Hello\\0\\0\\0","Hello\\0 World",8 ) => %d\n",memcmp( "Hello\0\0\0","Hello\0 World",8 ));    
37. printf("memcmp( "Hello World","hello World",11 ) => %d\n",memcmp( "Hello World","hello World",11 ));    
38.  
39. return 0;    40.    }

Conclusion:

So, in this tutorial, we have seen how strings can be compared in various ways. As we have seen, the stringCompare() function returns -1 for unequal strings, but this can be modified so that it returns ASCII value of mismatched character. You can use it in your code, which is best suited for you.

About the author

Bamdeb Ghosh

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python.