Compare String C – Program

Compare String C – Program

Write a C Program to check given string are equal or not equal.

#include<stdio.h>

int main()
{
 char str1[100], str2[100];
 int i,a=0;
 printf("Please enter the first string: ");
gets(str1); 

 printf("\nPlease enter the second string: ");
 gets(str2); 

 for(i=0;str1[i]!=NULL&&str2[i]!=NULL;i++)
 {
  if(str1[i]!=str2[i])
   break;
 }
 a=str1[i]-str2[i];
 
 if(a==0)
  printf("\nThe strings are equal");
 else
  printf("\nThe strings are not equal");

 return 0;
}

The output of string compare C Program is :

Please enter the first string: meeraacademy
Please enter the second string: meeraacademy

The strings are equal

//////////////////////

Please enter the first string: Meera
Please enter the second string: meera

The strings are not equal

 

Leave a Reply

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