1

Solved below.

Original problem: How can I get comparisons against argv[] to be case-insensitive? Here is a code fragment:

if (std::string(argv[2]) == "HKCU") //Si escriben HKCU
{
    cout << "Has escrito HKCU" << endl;
}
else //Si no escriben la clave
{
    cout << "Debes especificar HCKU o HKLM" << endl;
}

If I pass the parameter "hkcu" the test does not work, I have to type "HKCU". If I compare for either "HKCU" or "hkcu" in the program either string will work.

EDIT: I had to use _stricmp (Using VS2013) this way:

if (_stricmp(argv[2], "HKCU") == 0)
Sergio Calderon
  • 837
  • 1
  • 13
  • 32

3 Answers3

3

If boost is an option then you can use iequals

if (boost::iequals(std::string(argv[2]), "HKCU")) {
  ...
}

Another option is to just use strcasecmp

if (0 == strcasecmp(argv[2], "HKCU")) { 
  ...
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    Why boost for something so simple? – Jesus Ramos Aug 13 '13 at 21:06
  • @JesusRamos no particular reason, i added the `stricmp` solution as well – JaredPar Aug 13 '13 at 21:07
  • 1
    `strcasecmp` is more portable than `stricmp` – zwol Aug 13 '13 at 21:07
  • @JaredPar it just seems odd (or maybe it's that apparently everyone seems boost is good to solve any problem) that anyone would suggest boost for just a mundane comparison like this? – Jesus Ramos Aug 13 '13 at 21:08
  • Missing a closing paren in the second example. – Crowman Aug 13 '13 at 21:13
  • @JaredPar thanks a lot but I tried the second one and I still have the same problem. I mean: If I type for example hkcu does not work. Had to type HKCU – Sergio Calderon Aug 13 '13 at 21:22
  • Got it. I had to use _stricmp 'cause the strcmp did not work, and stricmp was deprecated. Thanks a lot! – Sergio Calderon Aug 13 '13 at 21:42
  • @JesusRamos: You make it sound as if boost is this huge cumbersome tool. It's not. It's a tool belt with many many useful tools. And it's incredibly easy to install on most platforms. – Benjamin Lindley Aug 13 '13 at 21:55
  • @BenjaminLindley It's kind of a large tool belt for something as simple as this. – Jesus Ramos Aug 13 '13 at 22:26
  • @JesusRamos: Well, you don't install boost for just this one feature. But if you have boost, then this feature is available to you, and it deserves a mention. It's just as good as any of the other options mentioned here(better in my opinion), which are all non-standard. – Benjamin Lindley Aug 13 '13 at 22:34
3

You need to rethink your question. You don't want the 'string' to be case insensitive, but much rather your comparison to realize, that HKCU is the same as Hkcu or hKcU.

For this end, there's a number of options, one of which is the already mentioned function stricmp. Prototype is:

#include <string.h>
int stricmp(const char *string1, const char *string2);

Meaning, you'd use it like:

if(stricmp(argv[2], "HKCU") == 0) {
}

Another option is the strcasecmp function which operates similarly.

Hope this helps.

Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24
2

argv[] in a C/C++ program is just a string as passed in by the shell. You have to change the comparison in your program to be case-insensitive.

amrith
  • 953
  • 6
  • 17