C Program to Compare Two Strings

Here we will write a C program to compare two strings without using any predefined function and using strcmp() or strncmp() functions, for these functions the Required header file is <string.h>

C program to compare two strings without using string manipulation library functions

#include<stdio.h>
int main()
{
   char s1[50], s2[50];
   int i, j, length, flag=0;

   printf("Enter first string: ");
   fgets(s1,sizeof(s1),stdin);
   printf("Enter second string: ");
   fgets(s2,sizeof(s2),stdin);

   //find length of both string
   for(i=0;s1[i]!='\0';i++);
   length=i;
   for(j=0;s2[j]!='\0';j++);

   //compare length of both string
   if(i!=j)
   printf("Strings are not equivalent.");
   else
   {
     for(i=0; i<length; i++)
     {
       if(s1[i]!=s2[i])
       {
         flag=1;
         break;
       }
     }
     if(flag==1) printf("Strings are not equivalent.");
     else printf("Strings are equivalent.");
   }

   return 0;
 }

Output:-

Enter first string: KnowProgram
Enter second string: KnowProgram
Strings are equivalent.

Enter first string: Program
Enter second string: Programming
Strings are not equivalent.

In this program, two strings are entered by user. First, we find the length of both strings, if lengths are not equal then strings are not equivalent. Otherwise, string may or may not be equivalent. When lengths are equal then the program checks both string character by character, if any character of string1 is not equal to second string then the string is not equivalent. When all characters are equal then both strings are equal.

Comparision using strcmp() function

In C language, to compare string there are mainly two functions strcmp() and strncmp().

The strcmp() compares two strings, character by character. If the first character of both strings is equal then the next character of both strings are compared. This continues until the corresponding characters of two strings are different or a null character ‘\0’ is reached.

The strcmp() function compares two strings identified by the arguments and returns a value. It returns 0 if both are equal otherwise it returns the ASCII difference between the first non-matching characters in the strings.

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Return value from strcmp()

0if both strings are identical (equal).
-veif the ASCII value of the first unmatched character is less than the second.
+veif the ASCII value of the first unmatched character is greater than second.
#include<stdio.h>
#include<string.h>
int main()
{
   char str1[50];
   char str2[50];

   printf("Enter first string: ");
   fgets(str1, sizeof(str1), stdin);
   printf("Enter second string: ");
   fgets(str2, sizeof(str2), stdin);

   int n = strcmp(str1,str2);

   if(n>0)
   printf("The string1= %s is greater than string2=%s",str1,str2);
   else
   {
     if(n<0)
     printf("The string1= %s is lesser than string2= %s", str1, str2 );
     else
     printf("The string1= %s is equal to string2=%s", str1,str2);
   }
   return 0;
 }

Output:-

Enter first string: Know Program
Enter second string: Know program
The string1= Know Program
is lesser than string2=Know program

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *