How to compare two strings in C++?

In this article, we will discuss different ways to compare strings in C++.

Table Of Contents

Problem Description

Suppose we have two strings, and we have to compare these two strings. If the two strings are same, then we have to print “Equal”, otherwise we have to print “Not Equal”.

Sample Input 1

str1 = "ABCDE"
str2 = "MNO"

Output

Advertisements
Not Equal

Sample Input 2

str1 = "ABC"
str2 = "ABC"

Output

Equal

There are several ways to compare strings in C++. Let’s discuss them one by one.

Method 1: Using C++ Relational operator == to compare strings in C++

In this approach, the strings gets compared using relational operators and the appropriate result is printed

Time Complexity: O(min(n,m)) where n and m are the length of the strings.
Space Complexity: O(max(n,m)) where n and m are the length of the strings

Example 1

#include <iostream>
#include <string>

int main()
{
    std::string strValue1 = "ABCDE"; 
    std::string strValue2 = "MNO"; 

    // Compare two string values
    if (strValue1 == strValue2) {
        std::cout << "Equal" << std::endl;
    }
    else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}

Output

Not Equal

Both the string values were not same in this example.

Example 2

#include <iostream>
#include <string>

int main()
{
    std::string strValue1 = "ABC"; 
    std::string strValue2 = "ABC"; 

    // Compare two string values
    if (strValue1 == strValue2) {
        std::cout << "Equal" << std::endl;
    }
    else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}

Output

Equal

Both the string values were same in this example.

Method 2: Using string::Compare() to compare strings in C++

In this approach, the strings gets compared using string class’s member function compare(). The function compare() reduces a lot of extra processing, therefore it is generally advised to use it when we are doing substring comparison, otherwise both of the methods perform nearly in the same manner.

The string::compare() function accepts two strings as arguments, and returnes a numeric value. If it returns 0, than the two strings are equal otherwise the strings are not equal.

Time Complexity: O(min(n,m)) where n and m are the length of the strings
Space Complexity: O(max(n,m)) where n and m are the length of the strings

Let’s see an example,

#include <iostream>
#include <string>

int main()
{
    std::string strValue1 = "ABC"; 
    std::string strValue2 = "ABC"; 

    // Compare two string values
    if (strValue1.compare(strValue2) == 0) 
    {
        std::cout << "Equal" << std::endl;
    }
    else 
    {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}

Output

Equal

As all the characters in both the strings were similar, therefore the compare() function returned 0. It means both the strings are equal.

Method 3: Using strcmp() to compare strings in C++

In this approach, the strings gets compared using the standard strcmp() function, which is present in <cstring> file.

The strcmp() function accepts two const char pointers as arguments, and returnes a numeric value. If it returns 0, than the two strings are equal otherwise the strings are not equal.

Time Complexity: O(min(n,m)) where n and m are the length of the strings
Space Complexity: O(max(n,m)) where n and m are the length of the strings

Let’s see an example,

#include <iostream>
#include <cstring>

int main()
{
    std::string strValue1 = "ABC"; 
    std::string strValue2 = "ABC"; 

    // Compare two string values
    if (strcmp(strValue1.c_str(), strValue2.c_str()) == 0) 
    {
        std::cout << "Equal" << std::endl;
    }
    else 
    {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}

Output

Equal

As all the characters in both the strings were similar, therefore the compare() function returned 0. It means both the strings are equal.

Summary

Today, we learned about various methods for comparing two strings in c++. We also got to know the time complexity of each of the methods and how each method is used for comparing the strings. Thanks.

Advertisements

Thanks for reading.

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