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

C++ examples for STL:string

Description

Doing a Case-Insensitive String Search

Demo Code

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

using namespace std;

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

string::const_iterator caseInsFind(string& s, const string& p) {
   string tmp;//www.j ava  2 s . co m

   transform(p.begin(), p.end(), back_inserter(tmp), toupper);// Make the pattern upper-case
             

   return(search(s.begin(), s.end(),tmp.begin(), tmp.end(), caseInsCharCompSingle));// Return the iterator returned by search
                 
}

int main() {
   string s = "this is a test test test";
   string p = "TEST";
   string::const_iterator it = caseInsFind(s, p);

   if (it != s.end()) {
      cout << "Found it!\n";
   }
}

Result


Related Tutorials