4

I am quite new to C++ programming and am getting to know the basics by reading books. I came across two interesting functions strcmpi() and stricmp(). I know that both the functions compare strings lexicographically by ignoring the case of the strings. So I just wanted to know the differences between them.

Any help would be appreciated.

b4hand
  • 9,550
  • 4
  • 44
  • 49
  • Strings are sorted by their ascii table values, starting at the leftmost (first) character, and moving incrementally down. In the case that one string terminates before another (and they are otherwise the same), the shorter string is first. (AKA: "asdf" is less than "asdfQ".) http://benborowiec.com/wp-content/uploads/2011/07/better_ascii_table.jpg – druckermanly Dec 13 '14 at 04:35
  • @user2899162 it has nothing to do with my question –  Dec 13 '14 at 04:38
  • @user2899162 i am asking why are there two separate functions to do the same thing –  Dec 13 '14 at 04:38
  • Refer here - http://stackoverflow.com/a/1964966/477522 – ajmartin Dec 13 '14 at 04:39
  • Wow, I completely misread. I was looking at a question from a friend, and reading your question, and gave you the wrong answer... I'll leave my stupidity up as a comment. My fault! strcmp is case sensitive, as in `C`, but the `i` in `strcmpi` means case insensitive-- as in, it's case insensitive. (I don't think it's part of the `C` standard, however.) – druckermanly Dec 13 '14 at 04:41
  • @user2899162 the what is stricmp? –  Dec 13 '14 at 04:44
  • Ah, I still can't read-- stricmp is just another name for strcmpi, according to a quick google search that lead me to http://msdn.microsoft.com/en-us/library/k59z8dwe.aspx – druckermanly Dec 13 '14 at 04:48
  • Strictly speaking those functions are from C. C++ tries to use std::string instead – Eric Dec 13 '14 at 04:48

2 Answers2

3

Both functions do the exact same thing (as long as you stick to comparing plain ASCII strings).

The problem is, neither is part of the ANSI C standard, so you can't be sure any of these will be available for a given compiler.

You can have yet other names for the same functionality. _strcmpi() for instance.

There is no standard case-insensitive comparison primitive in C/C++, so each compiler provides its own version with varying names.

The best "standard" variant would be the ISO C++ _stricmp, but I would not bet every compiler on the planet currently supports it.

The reason behind it is that case sensitivity is not as trivial a problem as it might seem, what with all the diacritics of various languages and the extended character encodings.

While plain ASCII string will always be compared the same way, you can expect differences in implementation when trying to compare UTF16 strings or other extended character sets.

Judging by this article, some C++ geeks seem to get a big kick rewriting their own version of it too.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
0

strcmpi and stricmp are case-insensitive versions of strcmp. They work identically in all other respects. _strcmpi and _stricmp are alternate names for strcmpi and stricmp. strcasecmp is an alias for strcmpi.

int strcmp (const char *string1, const char *string2);

int strcmpi (const char *string1, const char *string2);

int stricmp (const char *string1, const char *string2);

Varun Moghe
  • 74
  • 2
  • 3
  • 12