C++ String Equals

Two strings are said to be equal if they have same value at character level. Using C++, we can check if two strings are equal.

To check if two strings are equal, you can use Equal To == comparison operator, or compare() function of string class.

In this tutorial, we shall go through each of these processes with detailed C++ example programs.

C++ Check If Strings are Equal using Equal To Operator

Equal to == is a comparison operator using which we can compare two string and find if they are equal.

If the two strings are equal, equal to operator returns true. Otherwise, the operator returns false.

In the following two example programs, we initialize two strings with some values and check if these two strings are equal using equal to operator.

First let us take two strings with different values.

C++ Program

#include <iostream>
using namespace std;

int main() {
   string str1 = "hello";
   string str2 = "hi";

   if (str1 == str2) {
      cout << "The two strings are equal.";
   } else {
      cout << "The two strings are not equal.";
   }
}

Output

The two strings are not equal.

As the two strings are not equal, str1 == str2 returned false. So, else block is executed.

Now, let us provide same values for both the strings and check the output.

C++ Program

#include <iostream>
using namespace std;

int main() {
   string str1 = "hello";
   string str2 = "hello";

   if (str1 == str2) {
      cout << "The two strings are equal.";
   } else {
      cout << "The two strings are not equal.";
   }
}

Output

The two strings are equal.

As the two strings have same value, str1 == str2 returned true and the if block is executed.

C++ Check If Strings are Equal using compare()

compare() is a function in string class. compare() function can be called on a string, and accepts another string as an argument. compare() functions compares this string with the argument string.

If both the strings are equal, compare() returns integer value of zero. Else, it returns a non-zero value, positive or negative, based on the fact that this string is greater than or less than the argument string respectively.

Following is the syntax of compare() function.

str1.compare(str2)

where str1 and str2 are strings.

In the following two example programs, we initialize two strings with some values and check if these two strings are equal using compare() function.

First let us take two strings with different values.

C++ Program

#include <iostream>
using namespace std;

int main() {
   string str1 = "hello";
   string str2 = "hi";

   if (str1.compare(str2) == 0) {
      cout << "The two strings are equal.";
   } else {
      cout << "The two strings are not equal.";
   }
}

Output

The two strings are not equal.

As the two strings are not equal, str1.compare(str2) returned a non-zero value. So, else block is executed.

Now, let us provide same values for both the strings and check the output.

C++ Program

#include <iostream>
using namespace std;

int main() {
   string str1 = "hello";
   string str2 = "hello";

   if (str1.compare(str2) == 0) {
      cout << "The two strings are equal.";
   } else {
      cout << "The two strings are not equal.";
   }
}

Output

The two strings are equal.

As the two strings have same value, str1.compare(str2) returned true and the if block is executed.

Conclusion

In this C++ Tutorial, we learned how to check if two strings are equal or not using equal to assignment operator and compare() function.