In this C++ tutorial, you will learn how to compare strings, i.e., to check if one string is greater than the other, or if they are equal, using string::compare() function, with examples.

Compare Strings in C++

String comparison means to check if the given two string are equal, if first string is greater than second string, or if first string is less than second string.

std::string::compare() function in C++ compares two strings and returns a number. Based on the return value, we can find the result of string comparison as given in the following table.

Consider the expression str1.compare(str2), where we are comparing string str1 with string str2.

Return ValueString Comparison Description
0The two strings are equal.
Negative Valuestring str1 is less than the string str2.
Positive Valuestring str1 is greater than the string str2.

Examples

ADVERTISEMENT

1. Two strings are equal

In the following program, we take two strings: str1 and str2 such that both the string values are same. We shall compare them using compare() function and observe the result.

C++ Program

#include <iostream>
using namespace std;

int main() {
    string str1 = "apple";
    string str2 = "apple";
    int result = str1.compare(str2);
    cout << result << endl;
}

Output

0
Program ended with exit code: 0

compare() returned a value of 0.

2. The string is less than other string

In the following program, we take two strings: str1 and str2 such that str1 is less than str2 lexicographically. We shall compare them using compare() function and observe the result.

C++ Program

#include <iostream>
using namespace std;

int main() {
    string str1 = "apple";
    string str2 = "cherry";
    int result = str1.compare(str2);
    cout << result << endl;
}

Output

-2
Program ended with exit code: 0

compare() returned a negative value.

3. The string is greater than other string

In the following program, we take two strings: str1 and str2 such that str1 is less than str2 lexicographically. We shall compare them using compare() function and observe the result.

C++ Program

#include <iostream>
using namespace std;

int main() {
    string str1 = "mango";
    string str2 = "apple";
    int result = str1.compare(str2);
    cout << result << endl;
}

Output

12
Program ended with exit code: 0

compare() returned a positive value.

compare() return value

Please note that the value returned by compare() function is kind of a difference of second string from first string.

Single Program to make string comparison

In the following program, make use of if-else-if statement to check if given two strings are equal, greater or less, using compare() function.

C++ Program

#include <iostream>
using namespace std;

int main() {
    string str1 = "mango";
    string str2 = "apple";
    int comparison = str1.compare(str2);
    if (comparison == 0) {
        cout << "Strings are equal." << endl;
    } else if (comparison > 0) {
        cout << "First string is greater than the second." << endl;
    } else {
        cout << "First string is less than the second." << endl;
    }
}

Output

First string is greater than the second.
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned how to compare two string using compare() function, with the help of example programs.