Loading

C Program to Compare Two Strings

C - Program to Compare Two Strings


#include<stdio.h>
 
int compare_string(char[], char[]);
 
main()
{
    char first[100], second[100], result;
 
    printf("Enter first string\n");
    gets(first);
 
    printf("Enter second string\n");
    gets(second);
 
    result = compare_string(first, second);
 
    if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");
 
    return 0;
}
 
int compare(char a[], char b[])
{
   int c = 0;
 
   while( a[c] == b[c] )
   {
      if( a[c] == '\0' || b[c] == '\0' )
         break;
      c++;
   }
   if( a[c] == '\0' && b[c] == '\0' )
      return 0;
   else
      return -1;
} 

This entry was posted in . Bookmark the permalink.

One Response to C Program to Compare Two Strings