strcmp
int strcmp ( const char * string1, const char * string2 ); | string.h |
cplusplus.com |
Compare two strings.
Compares string1 to string2 character by character.
This function starts comparing the first character of each string. If they are
equal to each other continues with the following pair until the characters differ or
until end of string is reached.
Parameters.
Return Value.
Returns a value indicating the lexicographical relation between the strings:
return value description <0 string1 is less than string2 0 string1 is the same as string2 >0 string1 is greater than string2
Portability.
Defined in ANSI-C.
Example.
/* strcmp example */
#include <stdio.h>
#include <string.h>
main ()
{
char szKey[] = "apple";
char szInput[80];
do {
printf ("Which is my favourite fruit? ");
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
printf ("Correct answer!\n");
return 0;
}
Output:
Which is my favourite fruit? orange
Which is my favourite fruit? apple
Correct answer!
See also.
strncmp,
strrchr,
strspn,
memcmp