Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,988 questions

51,933 answers

573 users

How to perform case-insensitive two strings comparison in C++

2 Answers

0 votes
#include <iostream>
#include <cctype>
#include <string>

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

int main() {
    std::string str1 = "profession: c++ programmer";
    std::string str2 = "Profession: C++ PROGRAMMER";

    bool equals = equalsIgnoreCase(str1, str2);

    std::cout << (equals ? "true" : "false") << std::endl;
}


  
/*
run:
  
true
  
*/

 



answered Feb 24, 2025 by avibootz
edited Feb 24, 2025 by avibootz
0 votes
#include <algorithm>
#include <iostream>
#include <string>

bool equalsIgnoreCase(const std::string& str1, const std::string& str2) {
    std::string str1Lower = str1;
    std::string str2Lower = str2;
    
    std::transform(str1Lower.begin(), str1Lower.end(), str1Lower.begin(), ::tolower);
    std::transform(str2Lower.begin(), str2Lower.end(), str2Lower.begin(), ::tolower);
    
    return str1Lower == str2Lower;
}

int main() {
    std::string str1 = "profession: c++ programmer";
    std::string str2 = "Profession: C++ PROGRAMMER";

    bool equals = equalsIgnoreCase(str1, str2);

    std::cout << (equals ? "true" : "false") << std::endl;
}


  
/*
run:
  
true
  
*/

 



answered Feb 24, 2025 by avibootz

Related questions

1 answer 66 views
1 answer 78 views
1 answer 85 views
1 answer 75 views
1 answer 96 views
1 answer 68 views
...