r/cpp_questions • u/Weird-Promotion3026 • 4h ago
OPEN Need help
I’m trying to make a program where you input some text and the program will return those input into actions. My issue is that when I make an if statement, I need to include the input that the user would enter, but I’m trying to make it so that is not case sensitive. How do I do that?
If possible, I’m not looking just for the code, but the reasoning behind it, I’m trying to understand it, so if you can, explain it in the way that helped you understand. Thank you!
0
Upvotes
4
u/tangerinelion 4h ago
Either you normalize strings to lower case or upper case and then compare lower case strings with lower case strings or upper case with upper case so that it ends up being case insensitive or you use a string type that implements case insensitive comparison for you.
std::string doesn't have that, you can transform each character with std::tolower.
Microsoft's CString does have case insensitive comparison built-in, so it's a question of whether you want to use a string type that implements that for you or if you want to also learn how to implement it yourself.
Note that lower case versions of a string is actually really hard in general. It's not just A-Z to a-z, there are plenty of Unicode characters with lower case versions.
1
u/thingerish 4h ago
It's possible to make a case insensitive string class, Herb Sutter has a quick and dirty version
1
u/alfps 4h ago
General uppercasing is very complex because of text encoding and because in some countries uppercasing is non-systematic and may convert 1 character to 2 or vice versa.
However if you limit yourself to plain ASCII (which is restricted to the English alphabet A through Z) it's pretty straightforward.
The standard library offers some old C functions like
toupperbut they're a little dangerous so that one should write wrappers, and for ASCII it's just as easy to just write the requisite functionality directly:•
u/mredding 3h ago
This can get as hard as you want it.
How you do it is locale specific. Standard C++ only has direct support for the "C" aka "Classic" locale - that describes the compute environment of a PDP-7 and the C++ minimal character set. The standard does not actually mandate any particular encoding, so this often overlaps with ASCII, which also overlaps with the front of UTF-8 at the byte level.
That's really about all I can say.
std::tolowerwill work on this in the expected manner.All other locales - it depends on the locale.
std::tolowerwill Just Work(tm) for any installed locale, but does the locale even have case? Or worse - does it have case, and it's context sensitive? German has rules about which capital and lower letters you use; this means you can't just callstd::toloweron German, it requires a context aware grammar parser, andstd::tolowerdoes not maintain state and is not a parser.And what of your encoding? If you're using
char,std::tolowercan't see through a multi-byte sequence. What are you going to do? You can expand UTF-8 to a wide character like UTF-32, which was the original intent but no one does that. That might not work for all other encodings, because for as much as the UTF guys want it, the whole world isn't UTF.We can't get the industry to agree - UTF support in C++ sucks as it is, and I don't agree it SHOULD be a part of the standard, but that's a battle for another day.
But - if you're working within the basic character set that overlaps with ASCII, then this can be made about as easy as possible. If you want MORE than that, then you're looking into heavy template code, wide multi-byte character sets, locales, and 3rd party support like ICU.
And dude, we haven't even begun to discuss how hard this gets if you want to do it right in a language agnostic way. We'd have to get into i18n, l10n, and g11n...
The easiest thing to do is make your own case insensitive comparison functions between strings:
And WTF is that? This is a form of the "spaceship" 3-way comparison operator -
<=>. This can do any form of lesser, equal, or greater.Notice the lambda function, it converts the characters from the string views - which are of type
char, tounsigned char. This is required when callingstd::tolower, it preserves the byte representation and and prevents weird sign-extension bugs.std::toloweruses the globally installed C++ locale to lower the characters - so if you change that, your outcomes will vary.Now you can:
•
u/LazySapiens 3h ago
http://www.gotw.ca/gotw/029.htm