How to Compare Strings in C++

In this article, we are going to learn different ways of how to Compare Strings in C++ using built-in functions and operators with code examples.

How to Compare Strings in C++(3 Methods)


  • Using Relational Operators(==, !=) to compare strings in C++.
  • Using the strcmp() Function to compare Two Strings in C++.
  • Using the compare() Function to Compare Strings Ignoring Case.

1. Using Relational Operators(== , != ) to compare strings in C++


The first method to compare strings in C++ is by using simple relational operators that C++ has. We can make use of these operators on string class string objects. We can check the equality of comparison by using the “==” operator and also we can check strings non-equality by using the “!=” operator.

Let us understand both these operators use to compare the strings with the help of code examples.

C++ Program to compare string using Operators(== , != )

#include<iostream>
using namespace std;

int main() {

    string str1 = "Hello! World";
    string str2 = "Hello! World";
    string str3 = "Hellow! Universe";

    if (str1 == str2)
    {
        cout << "Both the Strings are same!!" << endl;
    }
    else
    {
        cout << "Both the Strings are Not same!!" << endl;
    }

    if (str1 != str3)
    {
        cout << "Both the Strings are Not same!!" << endl;
    }
    else
    {
        cout << "Both the Strings are same!!" << endl;
    }

    return 0;


}

Output

Both the Strings are same!!
Both the Strings are Not same!!

2. Using strcmp() Function to compare Two Strings in C++


The next method that we are going to learn to compare two strings in C++ is by using the strcmp() function.
This function is available in the
C library and it can be used to compare two strings. This function returns 0 if both the
passed strings are equal. This function works on case-sensitive strings as well.

so let us first check the Syntax.

Syntax

int strcmp(const char* str1, const char* str2);

Parameters

  • str1 = First char array of C – style string that we want to conpare.
  • str2 = Second char array of C – style string that we want to compare.

Now let us check this function in use in the below code example.

C++ Program to compare string using strcmp( ) function

#include<iostream>
#include<string.h>

using namespace std;

int main() {
    string str1 = "Hello! World";
    string str2 = "HeLLO! World";
    string str3 = "Hellow! Universe";

    if (strcmp(str1.c_str(), str2.c_str()) == 0)
    {
        cout << "Both the Strings are same!!" << endl;
    }
    else
    {
        cout << "Both the Strings are Not same!!" << endl;
    }

    if (strcmp(str1.c_str(), str3.c_str()) == 0)
    {
        cout << "Both the Strings are same!!" << endl;
    }
    else
    {
        cout << "Both the Strings are Not same!!" << endl;
    }

    return 0;

}

Output

Both the Strings are Not same!!
Both the Strings are Not same!!

3. Using compare() Function to Compare strings Ignoring Case


Another function that we have can also be used to compare two strings for equality. This function gives us some more
information but not just equality. This function can also tell us which string is smaller or bigger.

So let us understand about compare() function.

Syntax

int compare(const string & str) const;

Return Values

  • This function returns an integer return value. This value helps us determine the results of this function.
    • If return value is 0 , it means strings are equal.
    • If return value is negative , it means first string is smaller than second string.
    • If return value is positive(greater than 0) , it means second string is bigger than first string.

The calling string is the first string and the argument string is the second string. Let us understand this with the help of the below code example.

C++ Program to compare strings ignorance case

#include<iostream>
using namespace std;

int main() {
    string str1 = "Hello! World";
    string str2 = "HeLLO! World";
    string str3 = "Hellow! Universe";

    if (str1.compare(str2) == 0)
    {
        cout << "Both the Strings are same!!" << endl;
    }
    else
    {
        if(str1.compare(str2) < 0)
            cout << "str1 is smaller than str2" << endl;
        else
            cout << "str1 is bigger than str2" << endl;
    }

    if (str1.compare(str3) == 0)
    {
        cout << "Both the Strings are same!!" << endl;
    }
    else
    {
        if (str1.compare(str3) < 0)
            cout << "str1 is smaller than str3" << endl;
        else
            cout << "str1 is bigger than str3" << endl;
    }

    return 0;
}

Output

str1 is bigger than str2
str1 is smaller than str3

Summary

In this post, we have learned different ways of how to compare strings in C++.