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
...