Comparing strings in C++ determines whether two strings are equal or which one comes first in lexicographical order.
- String comparison is case-sensitive and compares characters from left to right.
- Relational operators and compare() can be used to compare std::string objects.
Examples: Given two strings, how to check if the two strings are equal or not.
Input : ABCD, XYZ
Output : ABCD is not equal to XYZ
XYZ is greater than ABCD
Input : Geeks, forGeeks
Output : Geeks is not equal to forGeeks
forGeeks is greater than Geeks
Note: String comparison in C++ is case-sensitive and follows lexicographical order, so uppercase and lowercase characters can produce different comparison results.
Approaches to Compare Two Strings
The following two approaches can be used to compare std::string objects in C++:
1. Comparing Strings Using Relational Operators
Relational operators such as ==, !=, <, >, <=, and >= can directly compare two std::string objects.
For example, s1 < s2 checks whether s1 comes before s2 lexicographically, while s1 == s2 checks whether both strings are equal.
#include <iostream>
#include <string>
using namespace std;
void relationalOperation(const string& s1, const string& s2)
{
if (s1 != s2)
{
cout << s1 << " is not equal to " << s2 << endl;
if (s1 > s2)
cout << s1 << " is greater than " << s2 << endl;
else
cout << s2 << " is greater than " << s1 << endl;
}
else
cout << s1 << " is equal to " << s2 << endl;
}
int main()
{
string s1("Geeks");
string s2("forGeeks");
relationalOperation(s1, s2);
string s3("Geeks");
string s4("Geeks");
relationalOperation(s3, s4);
return 0;
}
Output
Geeks is not equal to forGeeks forGeeks is greater than Geeks Geeks is equal to Geeks
Explanation: The comparison is case-sensitive. For example, "geeks" is greater than "forGeeks" because the first characters g and f are compared first, and g comes after f.
2. Comparing Strings Using compare()
The compare() function compares two strings lexicographically and returns:
- A value less than 0 if the first string is smaller.
- 0 if both strings are equal.
- A value greater than 0 if the first string is greater.
#include <iostream>
#include <string>
using namespace std;
void compareFunction(const string& s1, const string& s2)
{
int result = s1.compare(s2);
if (result != 0)
{
cout << s1 << " is not equal to " << s2 << endl;
if (result > 0)
cout << s1 << " is greater than " << s2 << endl;
else
cout << s2 << " is greater than " << s1 << endl;
}
else
cout << s1 << " is equal to " << s2 << endl;
}
int main()
{
string s1("Geeks");
string s2("forGeeks");
compareFunction(s1, s2);
string s3("Geeks");
string s4("Geeks");
compareFunction(s3, s4);
return 0;
}
Output
Geeks is not equal to forGeeks forGeeks is greater than Geeks Geeks is equal to Geeks
Relational Operators Vs compare()
- Relational operators return a Boolean value, while compare() returns a negative, zero, or positive integer.
- Relational operators are convenient for direct comparisons, while compare() also supports substring comparisons.
- compare() can compare specific portions of two strings without creating separate substrings.
Comparing Substrings Using compare()
The compare() function can compare specific parts of strings directly.
string str1 = "GeeksforGeeks";
string str2 = "Geeks";
if (str1.compare(5, 3, str2, 0, 3) == 0)
cout << "Equal";
else
cout << "Not equal";
Here, 3 characters starting from index 5 of str1 are compared with 3 characters starting from index 0 of str2. The compare() function is useful when specific portions of strings need to be compared without manually extracting them.