C++ String Comparison

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.

In this tutorial, we will go through an example, to demonstrate the string comparison using compare() function.

Examples

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.

This 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.

This 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 Comparison between Two Strings

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.