In this article we will discuss different ways to Compare strings in case insensitive manner.

Suppose we have two strings i.e.

Both the above strings should be equal if we compare them in case insensitive manner.

Let’s see how to compare them in case in-sensitive manner.

Case-insensitive string comparison in C++ using STL using equals()

std::equal() is an STL Algorithm i.e.

It accepts two range and compares all elements in the given range [startOfRange1,endOfRange1) with elements in the range beginning at startOfRange2 and returns true if all of the elements in both ranges are equal.
It uses binary predicate as callback to compare the elements of range.

Logic to Compare strings in case insensitive manner:

Iterate over all the elements in both the string and for each character in range1, check if its equal to corresponding element in range2. If not then check again by converting both of them in upper case. If they are still not equal then stop the iteration and return false as strings are not equal.

It’s necessary to check the size of strings before calling std::equal() because equals() does not check the size of range and can overrun.

Let’s use above created function to compare two strings in case insensitive manner.

Output:

Case-insensitive string comparison in using C++11 lambda function and equals()

Logic is same as above, use std::equals() but instead of another global function use lambda function and make solution in single line i.e.

Case-insensitive string comparison in using Boost iequals()

Header required:

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.