Changing cases and case-insensitive comparison
This is a pretty common task. We have two non-Unicode or ANSI character strings:
#include <string> std::string str1 = "Thanks for reading me!"; std::string str2 = "Thanks for reading ME!";
We need to compare them in a case-insensitive manner. There are a lot of methods to do that, let's take a look at Boost's.
Getting ready
Basic knowledge of std::string is all we need here.
How to do it...
Here are different ways to do case-insensitive comparisons:
- The most simple one is:
#include <boost/algorithm/string/predicate.hpp>
const bool solution_1 = (
boost::iequals(str1, str2)
);- Using the Boost predicate and standard library method:
#include <boost/algorithm/string/compare.hpp>
#include <algorithm>
const bool solution_2 = (
str1.size() == str2.size() && std::equal(
str1.begin(),
str1.end(),
str2.begin(),
boost::is_iequal()
)
);- Making a lowercase copy of both the strings:
...