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<iostream>
#include<cstring>
using namespace std;
int main()
{
char first[10] = "Welcome";
char second[10] = "Welcome";
char third[10] = "welcome";
cout<<"The content of first string : " <<first <<"\n";
cout<<"The content of second string : " <<second <<"\n";
// comparing the characters of first string with the characters in second string
int result = strcmp(first, second);
cout<<"String comparison between " <<first <<" and " <<second <<" : " <<result <<"\n";
// comparing the characters of first string with the characters in second string
result = strcmp(second, third);
cout<<"String comparison between " <<second <<" and " <<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<iostream>
#include<cstring>
using namespace std;
int main()
{
char first[15] = "Sky is blue";
char second[15] = "Sky is orange";
cout<< "The content of first string : " << first << "\n";
cout<< "The content of second string : " << second << "\n";
// comparing the characters of first string with the characters in second string
int result = strcmp(first, second);
cout<< "String comparison between " << first << " and " << 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