C Reference String Operation: strcmp()
The function strcmp() compares one string with another string. The function strcmp() returns a integer value.
Usage:
int strcmp( const char * target, const char * source);
If the two strings are equal then zero is returned.
If the target string is less than source then a value that is less then zero is returned.
If the target string is greater than source then a value that is greater then zero is returned.
strcmp() source code example:
#include<stdio.h>
#include<string.h>
main()
{
char line[100];
char line2[100];
printf("Input first word:");
scanf("%s", line);
printf("Input second word:");
scanf("%s", line2);
if (strcmp(line, line2) == 0)
printf ("equal\n");
if (strcmp(line, line2) < 0)
printf("less then equal\n");
if (strcmp(line, line2) > 0)
printf("greater then equal\n");
}
Output of the example:
Input first word:ab
Input second word:ab
equal
Input first word:aa
Input second word:ab
less then equal
Input first word:ab
Input second word:aa
greater then equal
Note: the program was executed three times to get the result of the output example.
This entry was posted in C Reference string.h Functions.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Tweet This! or use
to share this post with others.