Check if strings are equal in C++

This article will discuss different ways to check if two strings are equal in C++.

Table Of Contents

Check if strings are equal using == operator

We can use the operator == to compare two strings. If two strings are equal, then == operator will return true, otherwise returns false. Let’s see some examples,

Example 1:

#include <iostream>
#include <string>

int main()
{
    std::string first_str = "sample";
    std::string second_str = "sample";

    // Check if two strings are equal
    if ( first_str == second_str ) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Both the Strings are Equal

Here both the strings were precisely the same.

Example 2:

#include <iostream>
#include <string>

int main()
{
    std::string first_str = "last";
    std::string second_str = "sample";

    // Check if two strings are equal
    if ( first_str == second_str ) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Strings

It was a negative test because both the strings were not similar.

Check if strings are equal using compare() function

In C++, string class provides a member function compare(). It accepts a string as an argument and then compares it with the calling string object. It returns an integer i.e. 0 or < 0 or > 0,

  • If both the strings are equal, then it returns 0.
  • If calling string object is ordered before the string argument, then it returns an integer < 0.
  • If calling string object is ordered after the string argument, then it returns an integer > 0

We can check if two strings are equal by checking if compare() returns 0. For example,

Example 1:

#include <iostream>
#include <string>

int main()
{
    std::string first_str = "sample";
    std::string second_str = "sample";

    // Check if two strings are equal
    if ( first_str.compare(second_str) == 0 ) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Both the Strings are Equal

Here both the strings were precisely the same.

Example 2:

#include <iostream>
#include <string>

int main()
{
    std::string first_str = "last";
    std::string second_str = "sample";

    // Check if two strings are equal
    if ( first_str.compare(second_str) == 0 ) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Strings

Check if strings are equal using the equal() function

Standard Template Library in C++ provides a function std::equal(). It compares the two ranges for element-wise equality, and if all elements in two ranges are equal, it returns true, otherwise false. We can check if two strings are equal by providing both the strings as character range. For example,

Example 1:

#include <iostream>
#include <string>
#include<algorithm>


int main()
{
    std::string first_str = "sample";
    std::string second_str = "sample";

    // Check if two strings are equal
    bool result = std::equal( first_str.begin(),
                              first_str.end(),
                              second_str.begin(),
                              second_str.end());
    if ( result) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Both the Strings are Equal

Here both the strings were precisely the same.

Example 2:

#include <iostream>
#include <string>
#include<algorithm>


int main()
{
    std::string first_str = "last";
    std::string second_str = "sample";

    // Check if two strings are equal
    bool result = std::equal( first_str.begin(),
                              first_str.end(),
                              second_str.begin(),
                              second_str.end());
    if ( result) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Strings

Check if strings (char *) are equal using strcmp()

The strcmp() function accepts two character pointers as arguments and compares them character by character. It returns an integer i.e. 0 or < 0 or > 0,

  • If both the character pointer are equal, then it returns 0.
  • If the first string is ordered after the second string object, then it returns an integer > 0
  • If the string argument is ordered after calling string object, then it returns an integer > 0

We can use this to check if two strings (char *) are equal by checking if strcmp() returns 0. For example,

Example 1:

#include <iostream>
#include <cstring>

int main()
{
    const char* first = "sample";
    const char* second = "sample";

    // Check if two strings are equal
    if ( strcmp(first, second) == 0 ) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Both the Strings are Equal

Here both the strings were precisely the same.

Example 2:

#include <iostream>
#include <cstring>

int main()
{
    const char* first = "last";
    const char* second = "sample";

    // Check if two strings are equal
    if ( strcmp(first, second) == 0 ) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Strings

Check if strings (char *) are equal using compare()

If you are receiving the character pointers instead of the string object, then you can convert char * to a string object and then use compare() function for checking equality. For example,

Example 1:

#include <iostream>
#include <string>
#include <cstring>

int main()
{
    const char* first = "sample";
    const char* second = "sample";

    // Check if two strings are equal
    if ( std::string(first).compare(second) == 0 ) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Both the Strings are Equal

Here both the strings were precisely the same.

Example 2:

#include <iostream>
#include <string>
#include <cstring>

int main()
{
    const char* first = "sample";
    const char* second = "last";

    // Check if two strings are equal
    if ( std::string(first).compare(second) == 0 ) {
        std::cout<<"Both the Strings are Equal" <<std::endl;
    }
    else {
        std::cout<<"Strings are not Equal" <<std::endl;
    }

    return 0;
}

Output:

Strings

Summary:

We learned about different ways to check if two strings are equal in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top