strcmp(): Compare strings are return 0 if equal else return the ASCII difference between mismatching characters.
#include<string.h> int main() { char s1[20] = “hello”; char s2[20] = “Hello”; if(strcmp(s1,s2)==0) printf(“Strings are equal \n”); else printf(“String are not equal \n”); } |
Compare Strings without considering the Case using stricmp() function:
#include<stdio.h> #include<string.h> int main() { char s1[20] = “HELLO”; char s2[20] = “Hello”; if(stricmp(s1,s2)==0) printf(“Strings are equal \n”); else printf(“String are not equal \n”); } |
C program to compare two strings without using string functions: strcmp() function compares two string character by character until there is a mismatch occurs or end of the one of those strings reached whichever occurs first.
#include<stdio.h> int stringCompare(char[],char[]); int main() { char str1[100], str2[100]; int compare; printf(“Enter first string: “); gets(str1); printf(“Enter second string: “); gets(str2); compare = stringCompare(str1,str2); if(compare == 1) printf(“Both strings are equal.\n”); else printf(“Both strings are not equal\n”); return 0; } int stringCompare(char str1[],char str2[]) { int i=0,flag=0; while(str1[i]!=’\0′ && str2[i]!=’\0′){ if(str1[i]!=str2[i]){ flag=1; break; } i++; } if (flag==0 && str1[i]==’\0′ && str2[i]==’\0′) return 1; else return str1[i]-str2[i]; } |