How to Compare strings Ignoring Case in C++

C-Plus-Plus-String

In this article, we are going to learn different How to Compare strings Ignoring cases in C++. We will also learn how to
compare strings by ignoring the case in C++.

We will learn the below methods in this article.

  • Using strcasecmp() Function to Compare Two Strings Ignoring the Case.
  • Using custom function to Compare Two Strings Ignoring the Case.
  • Using the strncasecmp() Function to Compare Two Strings Ignoring the Case.

1. Using strcasecmp() Function to Compare strings Ignoring Case in C++


We can compare two strings in C++ by ignoring the case. The strings can be upper case or lower case. We have
strcasecmp() function in C standard library that can be used. This function is available in the header file. This function returns an integer return value. If this value is 0, it means strings are equal otherwise not equal.

C++ Program to compare string ignorance case using strcasecmp()

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

using namespace std;

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

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

    if (strcasecmp(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 same!!
Both the Strings are Not same!!

2. Using custom function and lambda to Compare Strings Ignoring Case.


Another method that can be used to compare two strings by ignoring the cases in C++ is by using custom functions.
We can create a function, that can convert the string to lowercase first and then do the comparison. This conversion makes the strings eligible for normal string comparison and we can use any of the normal functions
to compare like compare() or relational operators on strings.

let us understand this with the help of our example. In this example, we are converting the strings to lowercase and
doing the comparison. To do the lower case conversion, we are making use of the std library transform() function and a simple lambda function.

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

using namespace std;

std::string toLower(std::string s) {
    std::transform(s.begin(), s.end(), s.begin(),
        [](unsigned char c) { return std::tolower(c); });
    return s;
}

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

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

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

    return 0;
}

Output

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

3. Using strncasecmp() Function to Compare Strings Ignoring Case


Sometimes we have use cases where we need to compare partial strings and not full strings. For example, if we want to compare only the first few characters of a string to another string. In this case, we can make use of strncasecmp() function.

The strncasecmp() is used to compare a given number of characters from two strings by ignoring the case. This function takes 3 arguments, where the first argument is the first string, the second argument is the second string and the last argument is the number of first characters that we want to compare.

Let us understand this with the help of the below code example.

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

using namespace std;

#define CHARACTERCOMPARE  5

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

    if (strncasecmp(str1.c_str(), str2.c_str(), CHARACTERCOMPARE) == 0)
    {
        cout << "The first "<< CHARACTERCOMPARE << "characters of strings Matches.\n" << endl;
    }
    else
    {
        cout << "The first " << CHARACTERCOMPARE << "characters of strings Does Not Matches.\n" << endl;
    }

    if (strncasecmp(str1.c_str(), str3.c_str(), CHARACTERCOMPARE) == 0)
    {
        cout << "The first " << CHARACTERCOMPARE << "characters of strings Matches.\n" << endl;
    }
    else
    {
        cout << "The first " << CHARACTERCOMPARE << "characters of strings Does Not Matches.\n" << endl;
    }

    return 0;
}

Output

The first 5characters of strings Matches.

The first 5characters of strings Matches.