In this article we will discuss different ways to Compare strings in case insensitive manner.
Suppose we have two strings i.e.
std::string str1 = "Hi This is SamPle"; std::string str2 = "hi this is sample";
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.
bool equal (Iterator startOfRange1 , InputIterator endOfRange1, InputIterator startOfRange2, BinaryPredicate predicate);
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.
Frequently Asked:
- Check if a Char Array is equal to a string in C++
- Check if Char Array Starts with a string in C++
- Check if a Char Array is Null Terminated in C++
- Replace character in string at a particular index in C++
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.
#include <iostream> #include <string> #include <cctype> bool compareChar(char & c1, char & c2) { if (c1 == c2) return true; else if (std::toupper(c1) == std::toupper(c2)) return true; return false; } /* * Case Insensitive String Comparision */ bool caseInSensStringCompare(std::string & str1, std::string &str2) { return ( (str1.size() == str2.size() ) && std::equal(str1.begin(), str1.end(), str2.begin(), &compareChar) ); }
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.
std::string str1 = "Hi This is SamPle"; std::string str2 = "hi this is sample"; // Check if both strings are equal bool result = caseInSensStringCompare(str1, str2); // caseInSensStringCompare() returns true if both strings are equal in case insensitive manner if (result) std::cout << "Both Strings are equal" << std::endl; else std::cout << "Both Strings are not equal" << std::endl;
Output:
Both Strings are equal
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.
#include <string> #include <cctype> bool caseInSensStringCompareCpp11(std::string & str1, std::string &str2) { return ((str1.size() == str2.size()) && std::equal(str1.begin(), str1.end(), str2.begin(), [](char & c1, char & c2){ return (c1 == c2 || std::toupper(c1) == std::toupper(c2)); })); }
Case-insensitive string comparison in using Boost iequals()
std::string str1 = "Hi This is SamPle"; std::string str2 = "hi this is sample"; bool result = boost::iequals(str1, str2); // boost::iequals() returns true if both strings are equal in case insensitive manner if (result) std::cout << "Both Strings are equal" << std::endl; else std::cout << "Both Strings are not equal" << std::endl;
Header required:
#include <boost/algorithm/string/predicate.hpp>
Complete example is as follows,
#include <iostream> #include <string> #include <cctype> #include <boost/algorithm/string/predicate.hpp> bool compareChar(char & c1, char & c2) { if (c1 == c2) return true; else if (std::toupper(c1) == std::toupper(c2)) return true; return false; } /* * Case Insensitive String Comparision */ bool caseInSensStringCompare(std::string & str1, std::string &str2) { return ( (str1.size() == str2.size() ) && std::equal(str1.begin(), str1.end(), str2.begin(), &compareChar) ); } bool caseInSensStringCompareCpp11(std::string & str1, std::string &str2) { return ((str1.size() == str2.size()) && std::equal(str1.begin(), str1.end(), str2.begin(), [](char & c1, char & c2){ return (c1 == c2 || std::toupper(c1) == std::toupper(c2)); })); } int main() { std::string str1 = "Hi This is SamPle"; std::string str2 = "hi this is sample"; // Check if both strings are equal using STL and equals() bool result = caseInSensStringCompare(str1, str2); // caseInSensStringCompare() returns true if both strings are equal in case insensitive manner if (result) std::cout << "Both Strings are equal" << std::endl; else std::cout << "Both Strings are not equal" << std::endl; // Check if both strings are equal using cpp11 result = caseInSensStringCompare(str1, str2); // caseInSensStringCompare() returns true if both strings are exactly equal. if (result) std::cout << "Both Strings are equal" << std::endl; else std::cout << "Both Strings are not equal" << std::endl; result = boost::iequals(str1, str2); // boost::iequals() returns true if both strings are equal in case insensitive manner if (result) std::cout << "Both Strings are equal" << std::endl; else std::cout << "Both Strings are not equal" << std::endl; return 0; }
Output:
Both Strings are equal Both Strings are equal Both Strings are equal