String Compare – STRCMP

String Compare - STRCMP

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C programming concepts. You can learn C language at your own speed and time. One can learn concepts of C language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

String Compare - STRCMP
String Compare – STRCMP

Write a C program to String Compare – STRCMP

PROGRAM: String Compare – STRCMP

/* STRING COMPARE - STRCMP */

#include<stdio.h>

int main()
{
 char str1[100], str2[100];
 int i,d=0;
 printf("Please enter first string: ");
 // gets(str1); 
 // fgets is a better option over gets to read multiword string .
 fgets(str1, 100, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL;

 printf("Please enter second string: ");
 // gets(str2); 
 fgets(str2, 100, stdin);
 // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL;

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

 return 0;
}

Leave a Reply

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