III. The strcmp( ) Function
The strcmp( ) function compares the contents of one string to another string. It returns an
integer
value.
Syntax
strcmp(str1, str2);
Here str1 and str2 are the string constants or string variables.
The function returns 0 if they are
equal, returns a negative if str1 is less than str2, and returns a positive if str1 is greater than str2.
Remember that this comparison is case sensitive.
For example,
#include
#include
main( )
{
char name1[80], name2[80];
printf(“\nEnter your first string:”);
gets (name1);
printf(“\nEnter your second string:”);
gets (name2);
if (strcmp(name1, name2) = = 0)
printf(“Both strings are equal”);
else
printf(“Both strings are not equal”);
}
Do'stlaringiz bilan baham: