'Programs which accept uppercase and lowercase commands as input
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....
Solution 1:[1]
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.
Solution 2:[2]
If you convert each letter to lowercase, you only need to check if it equals "help." C++ has a std::tolower function that could help you out.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Dr. Andrey Belkin |
