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,989 questions

51,934 answers

573 users

How to case insensitive string compare in C++

2 Answers

0 votes
#include <iostream>

inline bool CaseInsensitiveCharCompare(char ch1, char ch2) {
     return tolower(ch1) == tolower(ch2);
}
 
bool CaseInsensitiveCompare(const std::string& s1, const std::string& s2) {
     return (s1.size() == s2.size()) &&
                equal(s1.begin(), s1.end(), s2.begin(), CaseInsensitiveCharCompare);
}
 
int main()
{
    std::string s1 = "C++ Programming";
    std::string s2 = "c++ programming";
     
    std::cout << CaseInsensitiveCompare(s1, s2);
}
  
  
  
  
  
/*
run:
  
1
  
*/

 



answered Sep 27, 2022 by avibootz
edited Jun 14, 2023 by avibootz
0 votes
#include <iostream>

bool CaseInsensitiveCompare(const std::string& s1, const std::string& s2) {
    return std::equal(s1.begin(), s1.end(),
                      s2.begin(), s2.end(),
                      [](char s1, char s2) {
                          return tolower(s1) == tolower(s2);
                      });
}
 
int main()
{
    std::string s1 = "C++ Programming";
    std::string s2 = "c++ programmING";
     
    std::cout << CaseInsensitiveCompare(s1, s2);
}
  
  
  
  
/*
run:
  
1
  
*/

 



answered Jun 14, 2023 by avibootz
...