Software & Finance - Monthly Magazine Online

Volume 4 - Issue 8

August 2014

Case Insensitive Find String in STL

string::find(...) function is used to find the given string as the argument. But it is a case sensitive search. How can we do the case insensitive search. There are couple of methods which can use STL algorithm for each and every comparison. However the easy method would be converting both string to upper case and then do the comparison using STL string find function.



int StringFindNoCase(const std::string &s1, const std::string &s2, int start = 0)


    char *p1 = new char[s1.size() + 1];

    char *p2 = new char[s2.size() + 1];


    for(int i = 0; i < s1.size(); i++)

        p1[i] = toupper(s1[i]);

    p1[s1.size()] = '\0';

    for(int i = 0; i < s2.size(); i++)

        p2[i] = toupper(s2[i]);

    p2[s2.size()] = '\0';


    std::string str1 = p1;

    std::string str2 = p2;


    delete p1;

    delete p2;


    return str1.find(str2, start);