int strcmp(const char *str1 , const char *str2 );
Compares the string str1 to the string str2.
Returns zero if str1 and str2 are equal.
Returns less than zero or greater than zero if str1 is less than or greater than str2 respectively.
int strcmp ( const char * str1, const char * str2 );
This function has the following parameter.
Returns an integral value indicating the relationship between the strings.
#include <stdio.h>
#include <string.h>
/* www . jav a 2 s.co m*/
int main (){
char key[] = "apple";
char buffer[80];
do {
printf ("Guess my favorite fruit? ");
fflush (stdout);
scanf ("%79s",buffer);
} while (strcmp (key,buffer) != 0);
return 0;
}
The code above generates the following result.
#include <string.h>
#include <stdio.h>
/*from w ww .ja v a2 s . c om*/
void demo(const char* lhs, const char* rhs)
{
int rc = strcmp(lhs, rhs);
if(rc == 0)
printf("[%s] equals [%s]\n", lhs, rhs);
else if(rc < 0)
printf("[%s] precedes [%s]\n", lhs, rhs);
else if(rc > 0)
printf("[%s] follows [%s]\n", lhs, rhs);
}
int main(void)
{
const char* string = "Hello World!";
demo(string, "Hello!");
demo(string, "Hello");
demo(string, "Hello ");
}
The code above generates the following result.