STL Algorithm
We can give our comparing function to make it work without pay attention to capital and small letter.
template< class InputIt1, class InputIt2, class Compare >
constexpr bool lexicographical_compare( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
Compare comp );
#include
#include
#include
#include
using namespace std;
int main()
{
string s1 = "Abcf";
string s2 = "AbCf";
// return true if the first range is lexicographically less than the second.
cout << "lexicographical_compare: " << lexicographical_compare( s1.begin(), s1.end(),
s2.begin(), s2.end() )
<< endl;
// ignore difference about capital and small letter, check whether they are same characters.
cout << "second result: " << lexicographical_compare( s1.begin(), s1.end(),
s2.begin(), s2.end(),
[](const char c1, const char c2){ return tolower(c1) == tolower(c2); } )
<< endl;
return 0;
}
lexicographical_compare: 0
second result: 1
Standard C Library
int strcasecmp(const char *s1, const char *s2);
It compares strings ignoring case.#include
using namespace std;
int main()
{
string s1 = "Abcf";
string s2 = "ABCf";
// return 0 if both charater is equal (ignore case)
cout << strcasecmp( s1.c_str(), s2.c_str() ) << endl;
return 0;
}