Doing a Case-Insensitive String Comparison - C++ STL

C++ examples for STL:string

Description

Doing a Case-Insensitive String Comparison

Demo Code

#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <cwctype>

 using namespace std;

 inline bool caseInsCharCompareN(char a, char b) {
   return(toupper(a) == toupper(b));
 }

 inline bool caseInsCharCompareW(wchar_t a, wchar_t b) {
   return(towupper(a) == towupper(b));
 }

 bool caseInsCompare(const string& s1, const string& s2) {
    return((s1.size() == s2.size()) &&
           equal(s1.begin(), s1.end(), s2.begin(), caseInsCharCompareN));
 }

 bool caseInsCompare(const wstring& s1, const wstring& s2) {
    return((s1.size() == s2.size()) &&
           equal(s1.begin(), s1.end(), s2.begin(), caseInsCharCompareW));
 }

 int main() {/*from w w  w.ja  v a 2s.  c o m*/
    string s1 = "THIS IS A TEST...";
    string s2 = "this is a test...";
    wstring ws1 = L"The END";
    wstring ws2 = L"the end test";

    if (caseInsCompare(s1, s2))
       cout << "Equal!\n";

    if (caseInsCompare(ws1, ws2))
       cout << "Equal!\n";
 }

Result


Related Tutorials