C Program To Compare Two Strings
Learn How To Compare Two Strings in C Programming Language. There are 3 different ways in which we can compare two given strings. It can be done using strcmp() function, pointers and without library function. The string comparison code also checks and compares the case of the characters within both the strings.
What is a String?
A String is represented by an Array of Characters in C Programming language. It is terminated using a Null character which is \0. In C programming, library functions for strings such as strcmp() are defined under string.h header file.
Must Read: C Program To Sort Name String in Descending Order
strcmp() function
- Returns negative value if string 1 < string 2
- Returns positive value if string 2 > string 1
- Returns zero if string 1 = string 2
Example
String 1: CodingAlpha
String 2: Codingalpha
Result: String 1 and String 2 are different
You can use gets() to take string input from the user. But, it is not deprecated. You can use a modified version of scanf() method to input a multi-word string. scanf(“%s”, str1) will store only a single word in the string variable whereas scanf(“%[^\n]s”, str1) will store multi words in the string variable.
Must Read: C Program To Reversal of a String
C Program For String Comparison using strcmp() Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h> #include<string.h> int main() { char string1[45], string2[45]; printf("\nEnter First String:\t"); scanf("%s", string1); printf("\nEnter Second String:\t"); scanf("%s", string2); if(strcmp(string1, string2) == 0) { printf("\nBoth the Strings are Equal\n"); } else { printf("\nThe Strings are not Equivalent\n"); } return 0; } |
Must Read: C Program To Convert String into Uppercase Characters
C Program To Compare Two Strings without using strcmp() Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> int string_comparison(char string1[], char string2[]) { int count = 0; while(string1[count] == string2[count]) { if(string1[count] == '\0' || string2[count] == '\0') { break; } else { count++; } } if(string1[count] == '\0' && string2[count] == '\0') { return 0; } else { return -1; } } int main() { int result; char string1[55], string2[55]; printf("\nEnter First String:\t"); scanf("%s", string1); printf("\nEnter Second String:\t"); scanf("%s", string2); result = string_comparison(string1, string2); if(result == 0) { printf("\nBoth the Strings are Equal\n"); } else { printf("\nThe Strings are not Equivalent\n"); } return 0; } |
Must Read: C Program To Sort Names in Alphabetical Order
C Program To Compare Two Strings using Pointers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include<stdio.h> int string_comparison(char *string1, char *string2) { while(*string1 == *string2) { if(*string1 == '\0' || *string2 == '\0') { break; } else { string1++; string2++; } } if(*string1 == '\0' && *string2 == '\0') { return 0; } else { return -1; } } int main() { char string1[55], string2[55], result; printf("Input string1 string\n"); scanf("%s", string1); printf("Input string2 string\n"); scanf("%s", string2); result = string_comparison(string1, string2); if(result == 0) { printf("\nBoth the Strings are Equal\n"); } else { printf("\nThe Strings are not Equivalent\n"); } return 0; } |
Must Read: C Program To Convert String into Lowercase Characters
Output

In case you get any compilation errors or any doubts in this C Program for String Comparison, Pointers and strcmp() function, let us know about it in the Comment Section below.
Thanks a ton. I wanted to check comparison for case sensitive strings.
I tried using gets() method for string comparison in my linux system. But, it’s giving an error.
Thanks for so many methods to compare strings in C.
There is one more string library function for comparing strings. It is strcmpi(). I think it checks the cases of the string as well.
In this case, strcmp() also checks the upper and lower cases of the strings.