C program to compare two strings – In this specific article, we will describe the various ways to compare two different strings in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The ways used in this particular piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using String Library Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
In the photo uploaded above, an example is showcased. A string needs to be entered first.
The string that is entered in the first case is as follows:
string1: welcome
Then, enter the second string that you want to compare with the first one.
string2: welcome
As you can see, both the strings are absolutely equal in nature.
Thus, the numerous methods to do the same in C programming are as follows:
Using Standard Method
- Read the entered strings s1 and s2 using gets(String) function and initialize to s1,s2.
2) First, check the lengths of two strings. If lengths of two strings are equal then only we can compare the strings. Compare each element of string s1 with each element of string s2 as follows.
a) for loop iterates with the structure for(i=0;s2[i]!=’\0′;i++)
If the element of string s1 is equal to the element of string s2 then increase the c value by 1. Repeat this step until the last character of s2 becomes null.
b) If c value is equal to i value,i.e each character of s1 is equal to each character of s2. Then print both the strings are equal. Otherwise print both the strings are not equal.
3) If the lengths of the two strings are not equal then print both the strings are not equal.
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 |
#include <stdio.h> #include <string.h> int main() { char s1[1000],s2[1000]; int i,c=0; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); if(strlen(s1)==strlen(s2)) { for(i=0;s2[i]!='\0';i++) { if(s1[i]==s2[i]) c++; } if(c==i) printf("strings are equal"); else printf("strings are not equal"); } else printf("strings are not equal"); return 0; } |
Output:
1 2 3 4 5 6 7 |
Enter string1: goutham Enter string2: goutham strings are equal Enter string1: hello Enter string2: world strings are not equal |
Using Function
- The main() function calls the stringcompare(char *s1,char *s2) function to compare the two strings.
2) If the length of the two strings is equal then,
a) compare each character of the string s1 with each character of the string s2 using for loop with the structure for(i=0;s2[i];i++).
b) If a character of string s1 is equal to a character of string s2 then increase the c value. Repeat till the last element of the string s1.
If c is equal to i then print both the strings are equal.
3) If the lengths of two strings are not equal then print two strings are not equal.
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 |
#include <stdio.h> #include <string.h> int stringcompare(char *s1,char *s2) { int i,c=0; if(strlen(s1)==strlen(s2)) { for(i=0;s2[i];i++) { if(s1[i]==s2[i]) c++; } if(c==i) return 1; } return 0; } int main() { char s1[1000],s2[1000],c; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); c=stringcompare(s1,s2); if(c) printf("strings are equal"); else printf("strings are not equal"); return 0; } |
Output:
1 2 3 4 5 6 7 |
Enter string1: computer Enter string2: programming strings are not equal Enter string1: c programming Enter string2: c programming strings are equal |
Using Recursion
- The function stringcompare() calculates the lengths of two strings as l1=strlen(s1) and l2=strlen(s2).
2) If the length of string s1 is not equal to the length of string s2 then the function stringcompare() returns 0.
3) If two strings lengths are equal,
a) Then compare s1[i] with s2[i],if s1[i] equal to s2[i] then increase c value,i value.
b) The function calls itself as stringcompare(s1,s2).It calls itself until i<l1 becomes false.
4) If c is equal to i then this function returns 1 otherwise it returns 0.
5) If c=1 then main function prints two strings are equal,if c=0 it prints two strings are not equal.
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 |
#include <stdio.h> #include <string.h> int stringcompare(char *s1,char *s2) { static int i=0,l1=strlen(s1),l2=strlen(s2),c=0; if(l1!=l2) { return 0; } else if(i<l1) { if(s1[i]==s2[i]) c++; i++; stringcompare(s1,s2); } if(c==i) return 1; return 0; } int main() { char s1[1000],s2[1000],c; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); c=stringcompare(s1,s2); if(c) printf("strings are equal"); else printf("strings are not equal"); return 0; } |
Output:
1 2 3 |
Enter string1: hello Enter string2: hello strings are equal |
Using String Library Function
- The function strcmp(String1, String2) is the string library function to compare two strings. If both the strings are equal then it returns 1 otherwise it returns 0.
2) The function strcmp(String1, String2) is available in the string.h header file.
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 s1[1000],s2[1000]; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); if(!strcmp(s1,s2)) printf("strings are equal"); else printf("strings are not equal"); return 0; } |
Output:
1 2 3 |
Enter string1: welcome Enter string2: welcome strings are equal |