Search code examples
c++case-insensitivestring-comparison

Case insensitive string comparison C++


I know there are ways to do case ignore comparison that involve iterating through strings or one good one on SO needs another library. I need to put this on other computers that might not have it installed. Is there a way to use the standard libraries to do this? Right now I am just doing...

if (foo == "Bar" || foo == "bar")
{
cout << "foo is bar" << endl;
}

else if (foo == "Stack Overflow" || foo == "stack Overflow" || foo == "Stack overflow" || foo == "etc.")
{
cout << "I am too lazy to do the whole thing..." << endl;
}

This could drastically improve the readability and usability of my code. Thanks for reading this far.


Solution

  • strncasecmp

    The strcasecmp() function performs a byte-by-byte comparison of the strings s1 and s2, ignoring the case of the characters. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

    The strncasecmp() function is similar, except that it compares no more than n bytes of s1 and s2...