C program to compare two string using strcmp() function.
C program to compare two string using strcmp() function.
#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
char text1[MAX], text2[MAX];
int result;
printf("Enter first string: ");
gets(text1);
printf("Enter second string: ");
gets(text2);
result = strcmp(text1, text2);
if(result == 0)
{
printf("\n Both strings are equal.");
}
else if(result == -1)
{
printf("\n First string is lexicographically smaller than second.");
}
else
{
printf("\n First string is lexicographically greater than second.");
}
return 0;
}
Output:
Enter first string: language
Enter second string: language
Both strings are equal.
Enter first string: Language
Enter second string: language
First string is lexicographically smaller than second.
Enter first string: language
Enter second string: Language
First string is lexicographically greater than second.