Advertisement
int strcmp ( const char * str1, const char * str2 )
int value | description |
---|---|
0 | when characters in two strings are the same. |
1 | when the non-matching character in the first string is having an ASCII value larger than the character at the same index in second string. |
-1 | when the non-matching character in the first string is having an ASCII value smaller
than the character at the same index in second string. |
/* strcmp example */
#include<string.h>
#include<stdio.h>
int main()
{
char first[10] = "Welcome";
char second[10] = "Welcome";
char third[10] = "welcome";
printf("The content of first string : %s", first);
printf("\n");
printf("The content of second string : %s", second);
printf("\n");
/* comparing the characters of first string with the characters in second string */
int result = strcmp(first, second);
printf("String comparison between %s and %s gives : %d \n", first, second, result);
/* comparing the characters of first string with the characters in second string */
result = strcmp(second, third);
printf("String comparison between %s and %s gives : %d", second, third, result);
return 0;
}
The content of first string : Welcome
The content of second string : Welcome
String comparison between Welcome and Welcome gives : 0
String comparison between Welcome and welcome gives : -1
Advertisement
/* strcmp example */
#include<string.h>
#include<stdio.h>
int main()
{
char first[15] = "Sky is blue";
char second[15] = "Sky is orange";
printf("The content of first string : %s", first);
printf("\n");
printf("The content of second string : %s", second);
printf("\n");
/* comparing the characters of first string with the characters in second string */
int result = strcmp(first, second);
printf("String comparison between %s and %s gives : %d \n", first, second, result);
return 0;
}
The content of first string : Sky is orange
The content of second string : Sky is blue
String comparison between Sky is orange and Sky is blue gives : 1
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement