I'm trying to add a help/information box in my program that pops whenever someone type in a /h, /?, /help commands. I want to make sure that my program accepts all characters in both upper and lower case. From what I have, I can check the most frequent cases of these commands, but not all (ie. /HeLp). Looking for way to cover all bases. Here's my current code:
....
bool CheckParseArguments(LPWSTR* argv, int argc)
{
for (int i = 0; i <= argc; i )
{
const wchar_t* help[] = { L"/h", L"/H", L"/?", L"/Help", L"/HELP", L"/help"};
for (int h = 0; h <= 5; h )
if (argc == (i 1) && wcscmp(argv[i], help[h]) == 0)
{
MessageBoxW(NULL, L"Correct input is ...", L"Help", MB_OK);
return false;
}
}
.... continue with other checks....
CodePudding user response:
With the Microsoft compiler (which you seem to be using), you can use the function _wcsicmp
instead of wcscmp
to perform a case-insensitive compare.
Other platforms have similar functions, such as strcasecmp
and wcscasecmp
on Linux.
ISO C itself does not provide a function which performs a case-insensitive compare. However, it is possible to convert the entire string to lowercase using the function std::tolower
or std::towlower
, before performing the compare. Afterwards, you won't need a case-insensitive compare, but can perform a standard case-sensitive compare.
CodePudding user response:
If you convert each letter to lowercase, you only need to check if it equals "help." C has a toLower function that could help you out.