compare() is a public member function of string class. It compares the value of the string object (or a substring) to the sequence of characters specified by its arguments.
The compare() can process more than one argument for each string so that one can specify a substring by its index and by its length.
Return type : compare() returns an integer value rather than a Boolean value.
Different Syntaxes for string::compare() :
- Syntax 1: Compares the string *this with the string str.
int string::compare (const string& str) const
Returns:
0 : if both strings are equal.
A value < 0 : if *this is shorter than str or,
first character that doesn't match is smaller than str.
A value > 0 : if *this is longer than str or,
first character that doesn't match is greater
CPP
#include<iostream>
using namespace std;
void compareOperation(string s1, string s2)
{
if((s1.compare(s2)) < 0)
cout << s1 << " is smaller than " << s2 << endl;
if((s1.compare(s1)) == 0)
cout << s1 << " is equal to " << s1 << endl;
else
cout << "Strings didn't match ";
}
int main()
{
string s1("Geeks");
string s2("forGeeks");
compareOperation(s1, s2);
return 0;
}
|
Output:
Geeks is smaller than forGeeks
Geeks is equal to Geeks
- Syntax 2: Compares at most, len characters of string *this, starting with index idx with the string str.
int string::compare (size_type idx, size_type len, const string& str) const
Throws out_of_range if index > size().
CPP
#include<iostream>
using namespace std;
void compareOperation(string s1, string s2)
{
if((s2.compare(3, 5, s1)) == 0)
cout << "Here, "<< s1 << " are " << s2;
else
cout << "Strings didn't match ";
}
int main()
{
string s1("Geeks");
string s2("forGeeks");
compareOperation(s1, s2);
return 0;
}
|
Output:
Here, Geeks are forGeeks
- Syntax 3: Compares at most, len characters of string *this starting with index idx with at most, str_len characters of string str starting with index str_idx.
int string::compare (size_type idx, size_type len, const string&
str, size_type str_idx, size_type str_len) const
Throws out_of_range if idx > size().
Throws out_of_range if str_idx > str.size().
CPP
#include<iostream>
using namespace std;
void compareOperation(string s1, string s2)
{
if((s1.compare(0, 5, s2, 3, 5)) == 0)
cout << "Welcome to " << s1 << s2 << " World";
else
cout << "Strings didn't match ";
}
int main()
{
string s1("Geeks");
string s2("forGeeks");
compareOperation(s1, s2);
return 0;
}
|
Output:
Welcome, to GeeksforGeeks World
- Syntax 4: Compares the characters of string *this with the characters of the C-string cstr.
int string::compare (const char* cstr) const
CPP
#include<iostream>
using namespace std;
void compareOperation(string s1, string s2)
{
if((s1.compare("GeeksforGeeks")) < 0)
cout << s1 << " is smaller than string " << "GeeksforGeeks";
if((s2.compare("forGeeks")) == 0)
cout << endl << s2 << " is equal to string " << s2;
else
cout << "Strings didn't match ";
}
int main()
{
string s1("Geeks");
string s2("forGeeks");
compareOperation(s1, s2);
return 0;
}
|
Output:
Geeks is smaller than string GeeksforGeeks
forGeeks is equal to string forGeeks
- Syntax 5: Compares at most, len characters of string *this, starting with index idx with all characters of the C-string cstr.
int string::compare (size_type idx, size_type len, const char* cstr) const
Note that cstr may not be a null pointer (NULL).
CPP
#include<iostream>
using namespace std;
void compareOperation(string s1)
{
if((s1.compare(0, 5, "Geeks")) == 0)
cout << s1 << " are " << "awesome people";
else
cout << "Strings didn't match ";
}
int main()
{
string s1("Geeks");
compareOperation(s1);
return 0;
}
|
Output:
Geeks are awesome people
- Syntax 6: Compares, at most, len characters of string *this, starting with index idx with chars_len characters of the character array chars.
int string::compare (size_type idx, size_type len, const char* chars,
size_type chars_len)const
Note that chars must have at least chars_len characters. The characters may have arbitrary values. Thus, ‘\0’ has no special meaning.
CPP
#include<iostream>
using namespace std;
void compareOperation(string s1, string s2)
{
if((s1.compare(0, 5, "Geeks", 5)) == 0)
cout << "This is " << s1 << s2 ;
else
cout << "Strings didn't match ";
}
int main()
{
string s1("Geeks");
string s2("forGeeks");
compareOperation(s1, s2);
return 0;
}
|
Output:
This is GeeksforGeeks
If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 May, 2022
Like Article
Save Article