How to Compare Two Strings Ignoring the Case in C++

  1. Use the strcasecmp Function to Compare Two Strings Ignoring the Case
  2. Use the strncasecmp Function to Compare Two Strings Ignoring the Case
  3. Use Custom toLower Function and == Operator to Compare Two Strings Ignoring the Case

This article will demonstrate multiple methods about how to compare two strings while ignoring the case of letters in C++.

Use the strcasecmp Function to Compare Two Strings Ignoring the Case

strcasecmp is the C standard library function that can be included in the C++ source file using the <cstring> header. The function itself operates on a byte-by-byte basis and returns an integer less than or equal or greater than 0, as corresponding strings evaluate.

Namely, if the two strings ignored the case are equal, the return value is zero. In other scenarios, when the first different characters are found, they are compared with the values to their alphabet position, and the corresponding result is returned.

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

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    string text1 = "Hey! Mr. Tambourine man, play a song for me";
    string text2 = "hey! Mr. Tambourine man, PLAY a song for me";
    string text3 = "Hey! Mrs. Tambourine man, play a song for me";

    if (strcasecmp(text1.c_str(), text2.c_str()) == 0) {
        cout << "strings: text1 and text2 match." << endl;
    } else {
        cout << "strings: text1 and text2 don't match!" << endl;
    }

    if (strcasecmp(text1.c_str(), text3.c_str()) == 0) {
        cout << "strings: text1 and text3 match." << endl;
    } else {
        cout << "strings: text1 and text3 don't match!" << endl;
    }

    return EXIT_SUCCESS;
}

Output:

strings: text1 and text2 match.
strings: text1 and text3 don't match!

Use the strncasecmp Function to Compare Two Strings Ignoring the Case

strncasecmp is another variation of a similar function that can be utilized to compare a given number of characters from two strings by ignoring the case. The function takes the integer value for the maximum number of characters that need to be compared from the first char. The following example demonstrates how to compare the first 5 characters of two strings ignoring the case.

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

using std::cout; using std::cin;
using std::endl; using std::string;

constexpr int BYTES_TO_COMPARE = 5;

int main(){
    string text1 = "Hey! Mr. Tambourine man, play a song for me";
    string text3 = "hey! Mrs. Tambourine man, PLAY a song for me";

    if (strncasecmp(text1.c_str(), text3.c_str(), BYTES_TO_COMPARE) == 0) {
        printf("The first %d characters of strings: text1 and text3 match.\n", BYTES_TO_COMPARE);
    }

    return EXIT_SUCCESS;
}

Output:

The first 5 characters of strings: text1 and text3 match.

Use Custom toLower Function and == Operator to Compare Two Strings Ignoring the Case

Alternatively, we can convert strings to the same case and then use a simple == operator for comparison. As an arbitrary choice, this example offers the lowering of both strings with a user-defined function, which returns the string object. Finally, the if statement can contain the eloquent comparison expression.

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

using std::cout; using std::cin;
using std::endl; using std::string;

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 text1 = "Hey! Mr. Tambourine man, play a song for me";
    string text2 = "Hey! Mr. Tambourine man, play a song for me";
    string text3 = "Hey! Mrs. Tambourine man, play a song for me";

    if (toLower(text1) == toLower(text2)){
        cout << "strings: text1 and text2 match." << endl;
    } else {
        cout << "strings: text1 and text2 don't match!" << endl;
    }

    if (toLower(text1) == toLower(text3)){
        cout << "strings: text1 and text3 match." << endl;
    } else {
        cout << "strings: text1 and text3 don't match!" << endl;
    }

    return EXIT_SUCCESS;
}

Output:

strings: text1 and text2 match.
strings: text1 and text3 don't match!

Related Article - C++ String

  • How to Iterate Through a String in C++
  • How to Convert Char Array to String in C++
  • comments powered by Disqus