<p>What is the best way of doing case-insensitive string comparison in C++ without transforming a string to all uppercase or all lowercase?</p>

<p>Please indicate whether the methods are Unicode-friendly and how portable they are.</p>

[{'answer_id': 11653, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 3, 'selected': False, 'text': "<p>Assuming you are looking for a method and not a magic function that already exists, there is frankly no better way. We could all write code snippets with clever tricks for limited character sets, but at the end of the day at somepoint you have to convert the characters.</p>\n\n<p>The best approach for this conversion is to do so prior to the comparison. This allows you a good deal of flexibility when it comes to encoding schemes, which your actual comparison operator should be ignorant of.</p>\n\n<p>You can of course 'hide' this conversion behind your own string function or class, but you still need to convert the strings prior to comparison.</p>\n"}, {'answer_id': 11654, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 4, 'selected': False, 'text': '<p>Visual C++ string functions supporting unicode: <a href="http://msdn.microsoft.com/en-us/library/cc194799.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/cc194799.aspx</a></p>\n\n<p>the one you are probably looking for is <code>_wcsnicmp</code></p>\n'}, {'answer_id': 11669, 'author': 'Wedge', 'author_id': 332, 'author_profile': 'https://Stackoverflow.com/users/332', 'pm_score': 4, 'selected': False, 'text': '<p>FYI, <code>strcmp()</code> and <code>stricmp()</code> are vulnerable to buffer overflow, since they just process until they hit a null terminator. It\'s safer to use <code>_strncmp()</code> and <a href="http://msdn.microsoft.com/en-us/library/chd90w8e(VS.80).aspx" rel="noreferrer"><code>_strnicmp()</code></a>.</p>\n'}, {'answer_id': 11670, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve had good experience using the <a href="http://icu-project.org/userguide/" rel="nofollow noreferrer">International Components for Unicode libraries</a> - they\'re extremely powerful, and provide methods for conversion, locale support, date and time rendering, case mapping (which you don\'t seem to want), and <a href="http://icu-project.org/userguide/Collate_Intro.html" rel="nofollow noreferrer">collation</a>, which includes case- and accent-insensitive comparison (and more). I\'ve only used the C++ version of the libraries, but they appear to have a Java version as well. </p>\n\n<p>Methods exist to perform normalized compares as referred to by @Coincoin, and can even account for locale - for example (and this a sorting example, not strictly equality), traditionally in Spanish (in Spain), the letter combination "ll" sorts between "l" and "m", so "lz" &lt; "ll" &lt; "ma".</p>\n'}, {'answer_id': 11675, 'author': 'Coincoin', 'author_id': 42, 'author_profile': 'https://Stackoverflow.com/users/42', 'pm_score': 6, 'selected': False, 'text': '<p>Are you talking about a dumb case insensitive compare or a full normalized Unicode compare?</p>\n\n<p>A dumb compare will not find strings that might be the same but are not binary equal. </p>\n\n<p>Example:</p>\n\n<pre><code>U212B (ANGSTROM SIGN)\nU0041 (LATIN CAPITAL LETTER A) + U030A (COMBINING RING ABOVE)\nU00C5 (LATIN CAPITAL LETTER A WITH RING ABOVE).\n</code></pre>\n\n<p>Are all equivalent but they also have different binary representations.</p>\n\n<p>That said, <a href="http://unicode.org/reports/tr15/" rel="nofollow noreferrer">Unicode Normalization</a> should be a mandatory read especially if you plan on supporting Hangul, Thaï and other asian languages.</p>\n\n<p>Also, IBM pretty much patented most optimized Unicode algorithms and made them publicly available. They also maintain an implementation : <a href="http://site.icu-project.org/" rel="nofollow noreferrer">IBM ICU</a></p>\n'}, {'answer_id': 11679, 'author': 'Adam', 'author_id': 1366, 'author_profile': 'https://Stackoverflow.com/users/1366', 'pm_score': 4, 'selected': False, 'text': '<p>I\'m trying to cobble together a good answer from all the posts, so help me edit this:</p>\n\n<p>Here is a method of doing this, although it does transforming the strings, and is not Unicode friendly, it should be portable which is a plus:</p>\n\n<pre><code>bool caseInsensitiveStringCompare( const std::string&amp; str1, const std::string&amp; str2 ) {\n    std::string str1Cpy( str1 );\n    std::string str2Cpy( str2 );\n    std::transform( str1Cpy.begin(), str1Cpy.end(), str1Cpy.begin(), ::tolower );\n    std::transform( str2Cpy.begin(), str2Cpy.end(), str2Cpy.begin(), ::tolower );\n    return ( str1Cpy == str2Cpy );\n}\n</code></pre>\n\n<p>From what I have read this is more portable than stricmp() because stricmp() is not in fact part of the std library, but only implemented by most compiler vendors.</p>\n\n<p>To get a truly Unicode friendly implementation it appears you must go outside the std library. One good 3rd party library is the <a href="http://www.icu-project.org/" rel="noreferrer">IBM ICU (International Components for Unicode)</a></p>\n\n<p>Also <strong>boost::iequals</strong> provides a fairly good utility for doing this sort of comparison.</p>\n'}, {'answer_id': 11685, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 6, 'selected': False, 'text': '<p>If you are on a POSIX system, you can use <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcasecmp.html" rel="noreferrer">strcasecmp</a>.  This function is not part of standard C, though, nor is it available on Windows.  This will perform a case-insensitive comparison on 8-bit chars, so long as the locale is POSIX.  If the locale is not POSIX, the results are undefined (so it might do a localized compare, or it might not).  A wide-character equivalent is not available.</p>\n\n<p>Failing that, a large number of historic C library implementations have the functions stricmp() and strnicmp().  Visual C++ on Windows renamed all of these by prefixing them with an underscore because they aren’t part of the ANSI standard, so on that system they’re called <a href="http://msdn.microsoft.com/en-us/library/k59z8dwe.aspx" rel="noreferrer">_stricmp or _strnicmp</a>.  Some libraries may also have wide-character or multibyte equivalent functions (typically named e.g. wcsicmp, mbcsicmp and so on).</p>\n\n<p>C and C++ are both largely ignorant of internationalization issues, so there\'s no good solution to this problem, except to use a third-party library.  Check out <a href="http://www.icu-project.org/" rel="noreferrer">IBM ICU (International Components for Unicode)</a> if you need a robust library for C/C++.  ICU is for both Windows and Unix systems.</p>\n'}, {'answer_id': 27813, 'author': 'Shadow2531', 'author_id': 1697, 'author_profile': 'https://Stackoverflow.com/users/1697', 'pm_score': 5, 'selected': False, 'text': '<p>My first thought for a non-unicode version was to do something like this:</p>\n<pre><code>bool caseInsensitiveStringCompare(const string&amp; str1, const string&amp; str2) {\n    if (str1.size() != str2.size()) {\n        return false;\n    }\n    for (string::const_iterator c1 = str1.begin(), c2 = str2.begin(); c1 != str1.end(); ++c1, ++c2) {\n        if (tolower(static_cast&lt;unsigned char&gt;(*c1)) != tolower(static_cast&lt;unsigned char&gt;(*c2))) {\n            return false;\n        }\n    }\n    return true;\n}\n</code></pre>\n'}, {'answer_id': 297355, 'author': 'John Dibling', 'author_id': 241536, 'author_profile': 'https://Stackoverflow.com/users/241536', 'pm_score': 3, 'selected': False, 'text': '<p>I wrote a case-insensitive version of char_traits for use with std::basic_string in order to generate a std::string that is not case-sensitive when doing comparisons, searches, etc using the built-in std::basic_string member functions.</p>\n\n<p>So in other words, I wanted to do something like this.</p>\n\n<pre><code>std::string a = "Hello, World!";\nstd::string b = "hello, world!";\n\nassert( a == b );\n</code></pre>\n\n<p>...which std::string can\'t handle.  Here\'s the usage of my new char_traits:</p>\n\n<pre><code>std::istring a = "Hello, World!";\nstd::istring b = "hello, world!";\n\nassert( a == b );\n</code></pre>\n\n<p>...and here\'s the implementation:</p>\n\n<pre><code>/*  ---\n\n        Case-Insensitive char_traits for std::string\'s\n\n        Use:\n\n            To declare a std::string which preserves case but ignores case in comparisons &amp; search,\n            use the following syntax:\n\n                std::basic_string&lt;char, char_traits_nocase&lt;char&gt; &gt; noCaseString;\n\n            A typedef is declared below which simplifies this use for chars:\n\n                typedef std::basic_string&lt;char, char_traits_nocase&lt;char&gt; &gt; istring;\n\n    --- */\n\n    template&lt;class C&gt;\n    struct char_traits_nocase : public std::char_traits&lt;C&gt;\n    {\n        static bool eq( const C&amp; c1, const C&amp; c2 )\n        { \n            return ::toupper(c1) == ::toupper(c2); \n        }\n\n        static bool lt( const C&amp; c1, const C&amp; c2 )\n        { \n            return ::toupper(c1) &lt; ::toupper(c2);\n        }\n\n        static int compare( const C* s1, const C* s2, size_t N )\n        {\n            return _strnicmp(s1, s2, N);\n        }\n\n        static const char* find( const C* s, size_t N, const C&amp; a )\n        {\n            for( size_t i=0 ; i&lt;N ; ++i )\n            {\n                if( ::toupper(s[i]) == ::toupper(a) ) \n                    return s+i ;\n            }\n            return 0 ;\n        }\n\n        static bool eq_int_type( const int_type&amp; c1, const int_type&amp; c2 )\n        { \n            return ::toupper(c1) == ::toupper(c2) ; \n        }       \n    };\n\n    template&lt;&gt;\n    struct char_traits_nocase&lt;wchar_t&gt; : public std::char_traits&lt;wchar_t&gt;\n    {\n        static bool eq( const wchar_t&amp; c1, const wchar_t&amp; c2 )\n        { \n            return ::towupper(c1) == ::towupper(c2); \n        }\n\n        static bool lt( const wchar_t&amp; c1, const wchar_t&amp; c2 )\n        { \n            return ::towupper(c1) &lt; ::towupper(c2);\n        }\n\n        static int compare( const wchar_t* s1, const wchar_t* s2, size_t N )\n        {\n            return _wcsnicmp(s1, s2, N);\n        }\n\n        static const wchar_t* find( const wchar_t* s, size_t N, const wchar_t&amp; a )\n        {\n            for( size_t i=0 ; i&lt;N ; ++i )\n            {\n                if( ::towupper(s[i]) == ::towupper(a) ) \n                    return s+i ;\n            }\n            return 0 ;\n        }\n\n        static bool eq_int_type( const int_type&amp; c1, const int_type&amp; c2 )\n        { \n            return ::towupper(c1) == ::towupper(c2) ; \n        }       \n    };\n\n    typedef std::basic_string&lt;char, char_traits_nocase&lt;char&gt; &gt; istring;\n    typedef std::basic_string&lt;wchar_t, char_traits_nocase&lt;wchar_t&gt; &gt; iwstring;\n</code></pre>\n'}, {'answer_id': 315463, 'author': 'Rob', 'author_id': 9236, 'author_profile': 'https://Stackoverflow.com/users/9236', 'pm_score': 9, 'selected': True, 'text': '<p>Boost includes a handy algorithm for this:</p>\n\n<pre><code>#include &lt;boost/algorithm/string.hpp&gt;\n// Or, for fewer header dependencies:\n//#include &lt;boost/algorithm/string/predicate.hpp&gt;\n\nstd::string str1 = "hello, world!";\nstd::string str2 = "HELLO, WORLD!";\n\nif (boost::iequals(str1, str2))\n{\n    // Strings are identical\n}\n</code></pre>\n'}, {'answer_id': 316573, 'author': 'Johann Gerell', 'author_id': 6345, 'author_profile': 'https://Stackoverflow.com/users/6345', 'pm_score': 2, 'selected': False, 'text': "<p>Just a note on whatever method you finally choose, if that method happens to include the use of <code>strcmp</code> that some answers suggest:</p>\n\n<p><code>strcmp</code> doesn't work with Unicode data in general. In general, it doesn't even work with byte-based Unicode encodings, such as utf-8, since <code>strcmp</code> only makes byte-per-byte comparisons and Unicode code points encoded in utf-8 can take more than 1 byte. The only specific Unicode case <code>strcmp</code> properly handle is when a string encoded with a byte-based encoding contains only code points below U+00FF - then the byte-per-byte comparison is enough.</p>\n"}, {'answer_id': 332713, 'author': 'bradtgmurray', 'author_id': 1546, 'author_profile': 'https://Stackoverflow.com/users/1546', 'pm_score': 5, 'selected': False, 'text': "<p>You can use <code>strcasecmp</code> on Unix, or <code>stricmp</code> on Windows.</p>\n\n<p>One thing that hasn't been mentioned so far is that if you are using stl strings with these methods, it's useful to first compare the length of the two strings, since this information is already available to you in the string class. This could prevent doing the costly string comparison if the two strings you are comparing aren't even the same length in the first place.</p>\n"}, {'answer_id': 2886502, 'author': 'Dean Harding', 'author_id': 241462, 'author_profile': 'https://Stackoverflow.com/users/241462', 'pm_score': 4, 'selected': False, 'text': '<p>The <a href="http://www.boost.org/doc/libs/1_43_0/doc/html/string_algo.html" rel="noreferrer">Boost.String</a> library has a lot of algorithms for doing case-insenstive comparisons and so on.</p>\n\n<p>You could implement your own, but why bother when it\'s already been done?</p>\n'}, {'answer_id': 2886589, 'author': 'wilhelmtell', 'author_id': 456, 'author_profile': 'https://Stackoverflow.com/users/456', 'pm_score': 7, 'selected': False, 'text': '<p>Take advantage of the standard <code>char_traits</code>. Recall that a <code>std::string</code> is in fact a typedef for <code>std::basic_string&lt;char&gt;</code>, or more explicitly, <code>std::basic_string&lt;char, std::char_traits&lt;char&gt; &gt;</code>. The <code>char_traits</code> type describes how characters compare, how they copy, how they cast etc. All you need to do is typedef a new string over <code>basic_string</code>, and provide it with your own custom <code>char_traits</code> that compare case insensitively.</p>\n\n<pre><code>struct ci_char_traits : public char_traits&lt;char&gt; {\n    static bool eq(char c1, char c2) { return toupper(c1) == toupper(c2); }\n    static bool ne(char c1, char c2) { return toupper(c1) != toupper(c2); }\n    static bool lt(char c1, char c2) { return toupper(c1) &lt;  toupper(c2); }\n    static int compare(const char* s1, const char* s2, size_t n) {\n        while( n-- != 0 ) {\n            if( toupper(*s1) &lt; toupper(*s2) ) return -1;\n            if( toupper(*s1) &gt; toupper(*s2) ) return 1;\n            ++s1; ++s2;\n        }\n        return 0;\n    }\n    static const char* find(const char* s, int n, char a) {\n        while( n-- &gt; 0 &amp;&amp; toupper(*s) != toupper(a) ) {\n            ++s;\n        }\n        return s;\n    }\n};\n\ntypedef std::basic_string&lt;char, ci_char_traits&gt; ci_string;\n</code></pre>\n\n<p>The details are on <a href="http://www.gotw.ca/gotw/029.htm" rel="noreferrer">Guru of The Week number 29</a>.</p>\n'}, {'answer_id': 4119881, 'author': 'Timmmm', 'author_id': 265521, 'author_profile': 'https://Stackoverflow.com/users/265521', 'pm_score': 7, 'selected': False, 'text': "<p>The trouble with boost is that you have to link with and depend on boost. Not easy in some cases (e.g. android).</p>\n<p>And using char_traits means <em>all</em> your comparisons are case insensitive, which isn't usually what you want.</p>\n<p>This should suffice. It should be reasonably efficient. Doesn't handle unicode or anything though.</p>\n<pre><code>bool iequals(const string&amp; a, const string&amp; b)\n{\n    unsigned int sz = a.size();\n    if (b.size() != sz)\n        return false;\n    for (unsigned int i = 0; i &lt; sz; ++i)\n        if (tolower(a[i]) != tolower(b[i]))\n            return false;\n    return true;\n}\n</code></pre>\n<p>Update: Bonus C++14 version (<code>#include &lt;algorithm&gt;</code>):</p>\n<pre><code>bool iequals(const string&amp; a, const string&amp; b)\n{\n    return std::equal(a.begin(), a.end(),\n                      b.begin(), b.end(),\n                      [](char a, char b) {\n                          return tolower(a) == tolower(b);\n                      });\n}\n</code></pre>\n<hr />\n<p>Update: C++20 version using <code>std::ranges</code>:</p>\n<pre><code>#include &lt;ranges&gt;\n#include &lt;algorithm&gt;\n#include &lt;string&gt;\n\nbool iequals(const std::string_view&amp; lhs, const std::string_view&amp; rhs) {\n    auto to_lower{ std::ranges::views::transform(std::tolower) };\n    return std::ranges::equal(lhs | to_lower, rhs | to_lower);\n}\n</code></pre>\n"}, {'answer_id': 10330109, 'author': 'Igor Milyakov', 'author_id': 1358161, 'author_profile': 'https://Stackoverflow.com/users/1358161', 'pm_score': 5, 'selected': False, 'text': '<p>boost::iequals is not utf-8 compatible in the case of string.\nYou can use <a href="http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/index.html">boost::locale</a>.</p>\n\n<pre><code>comparator&lt;char,collator_base::secondary&gt; cmpr;\ncout &lt;&lt; (cmpr(str1, str2) ? "str1 &lt; str2" : "str1 &gt;= str2") &lt;&lt; endl;\n</code></pre>\n\n<ul>\n<li>Primary -- ignore accents and character case, comparing base letters only. For example "facade" and "Façade" are the same.</li>\n<li>Secondary -- ignore character case but consider accents. "facade" and "façade" are different but "Façade" and "façade" are the same.</li>\n<li>Tertiary -- consider both case and accents: "Façade" and "façade" are different. Ignore punctuation.</li>\n<li>Quaternary -- consider all case, accents, and punctuation. The words must be identical in terms of Unicode representation.</li>\n<li>Identical -- as quaternary, but compare code points as well.</li>\n</ul>\n'}, {'answer_id': 15748771, 'author': 'michaelhanson', 'author_id': 448891, 'author_profile': 'https://Stackoverflow.com/users/448891', 'pm_score': 2, 'selected': False, 'text': '<p>As of early 2013, the ICU project, maintained by IBM, is a pretty good answer to this.</p>\n\n<p><a href="http://site.icu-project.org/" rel="nofollow">http://site.icu-project.org/</a></p>\n\n<p>ICU is a "complete, portable Unicode library that closely tracks industry standards."  For the specific problem of string comparison, the Collation object does what you want.</p>\n\n<p>The Mozilla Project adopted ICU for internationalization in Firefox in mid-2012; you can track the engineering discussion, including issues of build systems and data file size, here:</p>\n\n<ul>\n<li><a href="https://groups.google.com/forum/#!topic/mozilla.dev.platform/sVVpS2sKODw" rel="nofollow">https://groups.google.com/forum/#!topic/mozilla.dev.platform/sVVpS2sKODw</a></li>\n<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=724529" rel="nofollow">https://bugzilla.mozilla.org/show_bug.cgi?id=724529</a> (tracker)</li>\n<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=724531" rel="nofollow">https://bugzilla.mozilla.org/show_bug.cgi?id=724531</a> (build system)</li>\n</ul>\n'}, {'answer_id': 17330790, 'author': 'Neutrino', 'author_id': 954927, 'author_profile': 'https://Stackoverflow.com/users/954927', 'pm_score': 3, 'selected': False, 'text': "<p>For my basic case insensitive string comparison needs I prefer not to have to use an external library, nor do I want a separate string class with case insensitive traits that is incompatible with all my other strings.</p>\n\n<p>So what I've come up with is this:</p>\n\n\n\n<pre><code>bool icasecmp(const string&amp; l, const string&amp; r)\n{\n    return l.size() == r.size()\n        &amp;&amp; equal(l.cbegin(), l.cend(), r.cbegin(),\n            [](string::value_type l1, string::value_type r1)\n                { return toupper(l1) == toupper(r1); });\n}\n\nbool icasecmp(const wstring&amp; l, const wstring&amp; r)\n{\n    return l.size() == r.size()\n        &amp;&amp; equal(l.cbegin(), l.cend(), r.cbegin(),\n            [](wstring::value_type l1, wstring::value_type r1)\n                { return towupper(l1) == towupper(r1); });\n}\n</code></pre>\n\n<p>A simple function with one overload for char and another for whar_t. Doesn't use anything non-standard so should be fine on any platform.</p>\n\n<p>The equality comparison won't consider issues like variable length encoding and Unicode normalization, but basic_string has no support for that that I'm aware of anyway and it isn't normally an issue.</p>\n\n<p>In cases where more sophisticated lexicographical manipulation of text is required, then you simply have to use a third party library like Boost, which is to be expected.</p>\n"}, {'answer_id': 17866339, 'author': 'reubenjohn', 'author_id': 2110869, 'author_profile': 'https://Stackoverflow.com/users/2110869', 'pm_score': 2, 'selected': False, 'text': '<p>Just use <code>strcmp()</code> for case sensitive and <code>strcmpi()</code> or <code>stricmp()</code> for case insensitive comparison. Which are both in the header file <code>&lt;string.h&gt;</code></p>\n\n<p><strong>format:</strong></p>\n\n<pre><code>int strcmp(const char*,const char*);    //for case sensitive\nint strcmpi(const char*,const char*);   //for case insensitive\n</code></pre>\n\n<p><strong>Usage:</strong></p>\n\n<pre><code>string a="apple",b="ApPlE",c="ball";\nif(strcmpi(a.c_str(),b.c_str())==0)      //(if it is a match it will return 0)\n    cout&lt;&lt;a&lt;&lt;" and "&lt;&lt;b&lt;&lt;" are the same"&lt;&lt;"\\n";\nif(strcmpi(a.c_str(),b.c_str()&lt;0)\n    cout&lt;&lt;a[0]&lt;&lt;" comes before ball "&lt;&lt;b[0]&lt;&lt;", so "&lt;&lt;a&lt;&lt;" comes before "&lt;&lt;b;\n</code></pre>\n\n<p><strong>Output</strong></p>\n\n<p><em>apple and ApPlE are the same</em></p>\n\n<p><em>a comes before b, so apple comes before ball</em></p>\n'}, {'answer_id': 28869082, 'author': 'user4578093', 'author_id': 4578093, 'author_profile': 'https://Stackoverflow.com/users/4578093', 'pm_score': -1, 'selected': False, 'text': '<pre class="lang-c++ prettyprint-override"><code>bool insensitive_c_compare(char A, char B){\n  static char mid_c = (\'Z\' + \'a\') / 2 + \'Z\';\n  static char up2lo = \'A\' - \'a\'; /// the offset between upper and lowers\n\n  if (\'a\' &gt;= A and A &gt;= \'z\' or \'A\' &gt;= A and \'Z\' &gt;= A)\n      if (\'a\' &gt;= B and B &gt;= \'z\' or \'A\' &gt;= B and \'Z\' &gt;= B)\n      /// check that the character is infact a letter\n      /// (trying to turn a 3 into an E would not be pretty!)\n      {\n        if (A &gt; mid_c and B &gt; mid_c or A &lt; mid_c and B &lt; mid_c)\n        {\n          return A == B;\n        }\n        else\n        {\n          if (A &gt; mid_c)\n            A = A - \'a\' + \'A\'; \n          if (B &gt; mid_c)/// convert all uppercase letters to a lowercase ones\n            B = B - \'a\' + \'A\';\n          /// this could be changed to B = B + up2lo;\n          return A == B;\n        }\n      }\n}\n</code></pre>\n\n<p>this could probably be made much more efficient, but here is a bulky version with all its bits bare.</p>\n\n<p>not all that portable, but works well with whatever is on my computer (no idea, I am of pictures not words)</p>\n'}, {'answer_id': 28900301, 'author': 'smibe', 'author_id': 2239672, 'author_profile': 'https://Stackoverflow.com/users/2239672', 'pm_score': 0, 'selected': False, 'text': '<p>If you have to compare a source string more often with other strings one elegant solution is to use regex.</p>\n\n<pre><code>std::wstring first = L"Test";\nstd::wstring second = L"TEST";\n\nstd::wregex pattern(first, std::wregex::icase);\nbool isEqual = std::regex_match(second, pattern);\n</code></pre>\n'}, {'answer_id': 30193591, 'author': 'Craig Stoddard', 'author_id': 4891847, 'author_profile': 'https://Stackoverflow.com/users/4891847', 'pm_score': -1, 'selected': False, 'text': '<p>An easy way to compare strings that are only different by lowercase and capitalized characters is to do an ascii comparison. All capital and lowercase letters differ by 32 bits in the ascii table, using this information we have the following...</p>\n\n<pre><code>    for( int i = 0; i &lt; string2.length(); i++)\n    {\n       if (string1[i] == string2[i] || int(string1[i]) == int(string2[j])+32 ||int(string1[i]) == int(string2[i])-32) \n    {\n      count++;\n      continue;\n    }\n    else \n    {\n      break;\n    }\n    if(count == string2.length())\n    {\n      //then we have a match\n    }\n}\n</code></pre>\n'}, {'answer_id': 32619426, 'author': 'Brian Rodriguez', 'author_id': 4859885, 'author_profile': 'https://Stackoverflow.com/users/4859885', 'pm_score': 4, 'selected': False, 'text': '<p>See <a href="http://www.cplusplus.com/reference/algorithm/lexicographical_compare/" rel="noreferrer"><code>std::lexicographical_compare</code></a>:</p>\n\n<pre><code>// lexicographical_compare example\n#include &lt;iostream&gt;  // std::cout, std::boolalpha\n#include &lt;algorithm&gt;  // std::lexicographical_compare\n#include &lt;cctype&gt;  // std::tolower\n\n// a case-insensitive comparison function:\nbool mycomp (char c1, char c2) {\n    return std::tolower(c1) &lt; std::tolower(c2);\n}\n\nint main () {\n    char foo[] = "Apple";\n    char bar[] = "apartment";\n\n    std::cout &lt;&lt; std::boolalpha;\n\n    std::cout &lt;&lt; "Comparing foo and bar lexicographically (foo &lt; bar):\\n";\n\n    std::cout &lt;&lt; "Using default comparison (operator&lt;): ";\n    std::cout &lt;&lt; std::lexicographical_compare(foo, foo + 5, bar, bar + 9);\n    std::cout &lt;&lt; \'\\n\';\n\n    std::cout &lt;&lt; "Using mycomp as comparison object: ";\n    std::cout &lt;&lt; std::lexicographical_compare(foo, foo + 5, bar, bar + 9, mycomp);\n    std::cout &lt;&lt; \'\\n\';\n\n    return 0;\n}\n</code></pre>\n\n<p><a href="http://coliru.stacked-crooked.com/a/d38d82d1255c8f71" rel="noreferrer">Demo</a></p>\n'}, {'answer_id': 32833792, 'author': 'Simon Richter', 'author_id': 613064, 'author_profile': 'https://Stackoverflow.com/users/613064', 'pm_score': 2, 'selected': False, 'text': '<p>Late to the party, but here is a variant that uses <code>std::locale</code>, and thus correctly handles Turkish:</p>\n\n<pre><code>auto tolower = std::bind1st(\n    std::mem_fun(\n        &amp;std::ctype&lt;char&gt;::tolower),\n    &amp;std::use_facet&lt;std::ctype&lt;char&gt; &gt;(\n        std::locale()));\n</code></pre>\n\n<p>gives you a functor that uses the active locale to convert characters to lowercase, which you can then use via <code>std::transform</code> to generate lower-case strings:</p>\n\n<pre><code>std::string left = "fOo";\ntransform(left.begin(), left.end(), left.begin(), tolower);\n</code></pre>\n\n<p>This also works for <code>wchar_t</code> based strings.</p>\n'}, {'answer_id': 34739420, 'author': 'DavidS', 'author_id': 2338792, 'author_profile': 'https://Stackoverflow.com/users/2338792', 'pm_score': 3, 'selected': False, 'text': '<p>Doing this without using Boost can be done by getting the C string pointer with <code>c_str()</code> and using <code>strcasecmp</code>:</p>\n\n<pre><code>std::string str1 ="aBcD";\nstd::string str2 = "AbCd";;\nif (strcasecmp(str1.c_str(), str2.c_str()) == 0)\n{\n    //case insensitive equal \n}\n</code></pre>\n'}, {'answer_id': 39795447, 'author': 'kyb', 'author_id': 3743145, 'author_profile': 'https://Stackoverflow.com/users/3743145', 'pm_score': 4, 'selected': False, 'text': '<p>Short and nice. No other dependencies, than <em>extended</em> std C lib.</p>\n\n<pre><code>strcasecmp(str1.c_str(), str2.c_str()) == 0\n</code></pre>\n\n<p>returns <strong>true</strong> if <code>str1</code> and <code>str2</code> are equal.\n<code>strcasecmp</code> may not exist, there could be analogs <code>stricmp</code>, <code>strcmpi</code>, etc.</p>\n\n<p>Example code:</p>\n\n<pre><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;string.h&gt; //For strcasecmp(). Also could be found in &lt;mem.h&gt;\n\nusing namespace std;\n\n/// Simple wrapper\ninline bool str_ignoreCase_cmp(std::string const&amp; s1, std::string const&amp; s2) {\n    if(s1.length() != s2.length())\n        return false;  // optimization since std::string holds length in variable.\n    return strcasecmp(s1.c_str(), s2.c_str()) == 0;\n}\n\n/// Function object - comparator\nstruct StringCaseInsensetiveCompare {\n    bool operator()(std::string const&amp; s1, std::string const&amp; s2) {\n        if(s1.length() != s2.length())\n            return false;  // optimization since std::string holds length in variable.\n        return strcasecmp(s1.c_str(), s2.c_str()) == 0;\n    }\n    bool operator()(const char *s1, const char * s2){ \n        return strcasecmp(s1,s2)==0;\n    }\n};\n\n\n/// Convert bool to string\ninline char const* bool2str(bool b){ return b?"true":"false"; }\n\nint main()\n{\n    cout&lt;&lt; bool2str(strcasecmp("asd","AsD")==0) &lt;&lt;endl;\n    cout&lt;&lt; bool2str(strcasecmp(string{"aasd"}.c_str(),string{"AasD"}.c_str())==0) &lt;&lt;endl;\n    StringCaseInsensetiveCompare cmp;\n    cout&lt;&lt; bool2str(cmp("A","a")) &lt;&lt;endl;\n    cout&lt;&lt; bool2str(cmp(string{"Aaaa"},string{"aaaA"})) &lt;&lt;endl;\n    cout&lt;&lt; bool2str(str_ignoreCase_cmp(string{"Aaaa"},string{"aaaA"})) &lt;&lt;endl;\n    return 0;\n}\n</code></pre>\n\n<p>Output:</p>\n\n<pre><code>true\ntrue\ntrue\ntrue\ntrue\n</code></pre>\n'}, {'answer_id': 43226907, 'author': "vine'th", 'author_id': 478028, 'author_profile': 'https://Stackoverflow.com/users/478028', 'pm_score': 4, 'selected': False, 'text': '<pre><code>str1.size() == str2.size() &amp;&amp; std::equal(str1.begin(), str1.end(), str2.begin(), [](auto a, auto b){return std::tolower(a)==std::tolower(b);})\n</code></pre>\n\n<p>You can use the above code in C++14 if you are not in a position to use boost. You have to use <code>std::towlower</code> for wide chars.</p>\n'}, {'answer_id': 45899185, 'author': 'Jagadeesh Pulamarasetti', 'author_id': 4439625, 'author_profile': 'https://Stackoverflow.com/users/4439625', 'pm_score': 2, 'selected': False, 'text': "<p>Looks like above solutions aren't using compare method and implementing total again so here is my solution and hope it works for you (It's working fine).</p>\n\n<pre><code>#include&lt;iostream&gt;\n#include&lt;cstring&gt;\n#include&lt;cmath&gt;\nusing namespace std;\nstring tolow(string a)\n{\n    for(unsigned int i=0;i&lt;a.length();i++)\n    {\n        a[i]=tolower(a[i]);\n    }\n    return a;\n}\nint main()\n{\n    string str1,str2;\n    cin&gt;&gt;str1&gt;&gt;str2;\n    int temp=tolow(str1).compare(tolow(str2));\n    if(temp&gt;0)\n        cout&lt;&lt;1;\n    else if(temp==0)\n        cout&lt;&lt;0;\n    else\n        cout&lt;&lt;-1;\n}\n</code></pre>\n"}, {'answer_id': 49896180, 'author': 'DAme', 'author_id': 7814722, 'author_profile': 'https://Stackoverflow.com/users/7814722', 'pm_score': 2, 'selected': False, 'text': '<p>A simple way to compare two string in c++ (tested for windows) is using <strong>_stricmp</strong></p>\n\n<pre><code>// Case insensitive (could use equivalent _stricmp)  \nresult = _stricmp( string1, string2 );  \n</code></pre>\n\n<p>If you are looking to use with std::string, an example:</p>\n\n<pre><code>std::string s1 = string("Hello");\nif ( _stricmp(s1.c_str(), "HELLO") == 0)\n   std::cout &lt;&lt; "The string are equals.";\n</code></pre>\n\n<p>For more information here: <a href="https://msdn.microsoft.com/it-it/library/e0z9k731.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/it-it/library/e0z9k731.aspx</a></p>\n'}, {'answer_id': 50052242, 'author': 'Haseeb Mir', 'author_id': 6219626, 'author_profile': 'https://Stackoverflow.com/users/6219626', 'pm_score': 1, 'selected': False, 'text': '<p>If you don\'t want to use <strong>Boost library</strong> then here is solution to it using only C++ standard io header.</p>\n\n<pre><code>#include &lt;iostream&gt;\n\nstruct iequal\n{\n    bool operator()(int c1, int c2) const\n    {\n        // case insensitive comparison of two characters.\n        return std::toupper(c1) == std::toupper(c2);\n    }\n};\n\nbool iequals(const std::string&amp; str1, const std::string&amp; str2)\n{\n    // use std::equal() to compare range of characters using the functor above.\n    return std::equal(str1.begin(), str1.end(), str2.begin(), iequal());\n}\n\nint main(void)\n{\n    std::string str_1 = "HELLO";\n    std::string str_2 = "hello";\n\n    if(iequals(str_1,str_2))\n    {\n        std::cout&lt;&lt;"String are equal"&lt;&lt;std::endl;   \n    }\n\n    else\n    {\n        std::cout&lt;&lt;"String are not equal"&lt;&lt;std::endl;\n    }\n\n\n    return 0;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/11635', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1366/']