user4375224
user4375224

Reputation:

C++: String Functions

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.

Upvotes: 4

Views: 264

Answers (2)

kuroi neko
kuroi neko

Reputation: 8641

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.

Upvotes: 3

Varun Moghe
Varun Moghe

Reputation: 124

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);

Upvotes: 0

Related Questions