How to Ignore Text Case While Comparing Two Strings In C++?

9 minutes read

In C++, comparing two strings in a case-insensitive manner can be achieved by converting both strings to lowercase or uppercase before comparing. The standard library provides functions to handle string manipulations. Here is an example of how to ignore text case while comparing two strings:

  1. Firstly, include the necessary headers for string and character manipulation:
1
2
3
4
#include <iostream>
#include <string>
#include <algorithm> // for std::transform
#include <cctype> // for std::tolower, std::toupper


  1. Then, create a function that converts a string to lowercase or uppercase:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
void toLowercase(std::string& str) {
    std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
        return std::tolower(c);
    });
}

void toUppercase(std::string& str) {
    std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
        return std::toupper(c);
    });
}


  1. Finally, compare the strings after converting them to the desired case:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
int main() {
    std::string str1 = "Hello";
    std::string str2 = "hello";

    // Convert both strings to lowercase for case-insensitive comparison
    toLowercase(str1);
    toLowercase(str2);

    // Compare the lowercase strings
    if (str1 == str2) {
        std::cout << "The strings are equal." << std::endl;
    } else {
        std::cout << "The strings are not equal." << std::endl;
    }

    return 0;
}


In the above example, the two strings "Hello" and "hello" are compared in a case-insensitive manner. Both strings are converted to lowercase using the toLowercase function, and then compared using the == operator.


By transforming the strings to lowercase or uppercase before comparison, the comparison becomes independent of the original case of the strings.

Best C++ Books to Read in 2024

1
C# & C++: 5 Books in 1 - The #1 Coding Course from Beginner to Advanced (2023) (Computer Programming)

Rating is 5 out of 5

C# & C++: 5 Books in 1 - The #1 Coding Course from Beginner to Advanced (2023) (Computer Programming)

2
C Programming Language, 2nd Edition

Rating is 4.9 out of 5

C Programming Language, 2nd Edition

3
Effective C: An Introduction to Professional C Programming

Rating is 4.8 out of 5

Effective C: An Introduction to Professional C Programming

4
The C Programming Language

Rating is 4.6 out of 5

The C Programming Language

5
C Programming: A Modern Approach, 2nd Edition

Rating is 4.5 out of 5

C Programming: A Modern Approach, 2nd Edition

6
C Programming Absolute Beginner's Guide

Rating is 4.4 out of 5

C Programming Absolute Beginner's Guide

7
Learn C Programming: A beginner's guide to learning the most powerful and general-purpose programming language with ease, 2nd Edition

Rating is 4.3 out of 5

Learn C Programming: A beginner's guide to learning the most powerful and general-purpose programming language with ease, 2nd Edition

8
C Programming For Dummies (For Dummies (Computer/Tech))

Rating is 4.2 out of 5

C Programming For Dummies (For Dummies (Computer/Tech))


What is the method to do a case-insensitive comparison of strings in C++?

In C++, you can achieve a case-insensitive comparison of strings using the following method:

  1. Convert both strings to lowercase or uppercase to eliminate the case difference. This can be done using either the tolower() or toupper() functions from the header.
  2. Compare the modified strings using the appropriate comparison operator (==, !=, <, >, etc.).


Here's an example using the tolower() function to perform a case-insensitive equality check:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cctype>
#include <iostream>
#include <string>

bool caseInsensitiveEquals(const std::string& str1, const std::string& str2)
{
    if (str1.length() != str2.length()) {
        return false;
    }
    
    for (size_t i = 0; i < str1.length(); i++) {
        if (std::tolower(str1[i]) != std::tolower(str2[i])) {
            return false;
        }
    }
    
    return true;
}

int main()
{
    std::string str1 = "Hello";
    std::string str2 = "heLLo";
    
    if (caseInsensitiveEquals(str1, str2)) {
        std::cout << "Strings are case-insensitive equals" << std::endl;
    } else {
        std::cout << "Strings are different" << std::endl;
    }
    
    return 0;
}


This example converts both strings to lowercase using std::tolower() and then compares them character by character. If all characters are equal, the strings are considered case-insensitive equals.


How to ignore case sensitivity when comparing two strings in C++?

To ignore case sensitivity when comparing two strings in C++, you can convert both strings to lowercase (or uppercase) before comparing them. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <algorithm>  // for std::transform
#include <cctype>     // for std::tolower

int main() {
    std::string str1 = "Hello";
    std::string str2 = "hello";

    // Convert both strings to lowercase
    std::transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
    std::transform(str2.begin(), str2.end(), str2.begin(), ::tolower);

    // Compare the transformed strings
    if (str1 == str2) {
        std::cout << "Strings are equal." << std::endl;
    } else {
        std::cout << "Strings are not equal." << std::endl;
    }

    return 0;
}


In this example, we use the std::transform function along with the std::tolower function from the <cctype> library to convert each character in the strings to lowercase. Then, we compare the transformed strings using the == operator.


How to compare two strings without considering case in C++?

To compare two strings without considering the case in C++, you can convert both strings to lowercase or uppercase before performing the comparison. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cctype> // for toupper()

int main() {
    std::string str1 = "Hello";
    std::string str2 = "hello";

    // Convert both strings to uppercase
    for (char& c : str1) {
        c = std::toupper(c);
    }
    for (char& c : str2) {
        c = std::toupper(c);
    }

    // Compare the uppercase strings
    if (str1 == str2) {
        std::cout << "The strings are equal." << std::endl;
    } else {
        std::cout << "The strings are not equal." << std::endl;
    }

    return 0;
}


This program converts both str1 and str2 to uppercase using the std::toupper() function from the <cctype> library. Then, it compares the converted strings using the equality operator ==.


How to check if two strings are equal regardless of case in C++?

You can use the strcasecmp or stricmp function from the cstring library to compare two strings regardless of case in C++. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>

int main() {
    const char* str1 = "Hello";
    const char* str2 = "hello";

    int result = strcasecmp(str1, str2);

    if (result == 0) {
        std::cout << "Strings are equal." << std::endl;
    } else {
        std::cout << "Strings are not equal." << std::endl;
    }

    return 0;
}


Output:

1
Strings are equal.


Note that strcasecmp and stricmp function return 0 if the strings are equal ignoring case, and non-zero if the strings are not equal.

Facebook Twitter LinkedIn Telegram Pocket

Related Posts:

A Google employee John Muller confirmed on Twitter that the search engine could ignore Sitemap.xml files if they contain invalid URLs. At the same time, if the URLs were redirected and the content is loaded, then this will not happen. Otherwise, Google will su...
To store text file data into a vector in C++, you would need to perform the following steps:Include the necessary headers: #include #include #include #include Declare the vector to store the text file data: std::vector fileData; Open the text file: std::ifstre...
In Jinja2, you can mark strings as &#34;safe&#34; to prevent them from being escaped when rendered in a view or template. This is useful when you have a string that contains HTML or other special characters that you want to display as-is on the page.To mark a ...