2

Possible Duplicate:
Case insensitive string comparison in C++
C++ count and map

How to do case insensitive count in C++ without using tolower. I want to count words irrespective of their case, but do not want to use tolower. Any other solution in C++?

Community
  • 1
  • 1
user1035927
  • 1,653
  • 5
  • 17
  • 18

2 Answers2

2

You can use as std::map with a custom comparer function object that is case insensitive

struct StrCaseInsensitive
{
    bool operator() (const string& left , const string& right )
    {
        return _stricmp( left.c_str() , right.c_str() ) < 0;
    }
};

int main(void)
{
    char* input[] = { "Foo" , "bar" , "Bar" , "FOO" };
    std::map<string, int , StrCaseInsensitive> CountMap;

    for( int i = 0 ; i < 4; ++i )
    {
        CountMap[ input[i] ] += 1;
    }
    return 0;
}
parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
2

If you don't want to use tolower, there's always toupper, which works just as well.

You don't have to store the string in lowercase, you can just compare in lowercase.

Or you can use strcasecmp or stricmp if it's available.

Or you can use someone else's solution.

Community
  • 1
  • 1
tylerl
  • 30,197
  • 13
  • 80
  • 113