C examples for string.h:strcmp
function
<cstring> <string.h>
Compares the C string str1 to the C string str2, letter by letter.
int strcmp ( const char * str1, const char * str2 );
Parameter | Description |
---|---|
str1 | C string to be compared. |
str2 | C string to be compared. |
Returns an integral value indicating the relationship between the strings:
return value | indicates |
---|---|
<0 | ptr1 has lower value than ptr2 |
0 | the contents of both strings are equal |
>0 | ptr1 has higher value than ptr2 |
#include <stdio.h> #include <string.h> int main ()/*w ww . j a v a 2s. com*/ { char key[] = "apple"; char buffer[80]; do { printf ("Guess my favorite fruit(hint:apple)? "); fflush (stdout); scanf ("%79s",buffer); } while (strcmp (key,buffer) != 0); puts ("Correct answer!"); return 0; }