Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to ignore text case while comparing two strings in C++?

I am a beginner coder. I am trying to create a program which will compare two strings alphabetically. But It will ignore the text case. I am facing problem on it. How can I ignore the text case in C++?

#include <iostream>

using namespace std;

int main() {

    string a, b;

    cin >> a;
    cin >> b;

    if ( a > b) {
        cout << "1";
    }
    else if ( a < b) {
        cout << "-1";
    }
    else if (a == b) {
        cout << "0";
    }

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can convert both strings to lower case before the comparison via std::tolower:

for (auto& c : a) c = std::tolower(static_cast<unsigned char>(c));
for (auto& c : b) c = std::tolower(static_cast<unsigned char>(c));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading