+ 3

How to make string case insensitive?

String s="sololearn"; if(s==sololearn){ //statement 1 } if(s=="SoloLearn"{ //statement 2 } if(s=="Sololearn"{ //statement 3 } if(s=="SoLoLeArN"{ //statement 4 } here in this code only statement one will be executed... but if the string value will be case insensitive then all the statements will be executed. so please explain me what should I do so that all the statement will be executed? I need an answer to this question to modify my following code. https://code.sololearn.com/cKYjRQSB8IL3/?ref=app

13th Jul 2018, 4:54 PM
Suraj Jha
Suraj Jha - avatar
9 Réponses
+ 6
I'd force to lowercase before testing: #include <cctype> string lower(string s) { string r = s; for (int i = 0; i < r.length(); i++) r[i] = tolower(r[i]); return r; } if(lower(function)=="log")
13th Jul 2018, 7:01 PM
John Wells
John Wells - avatar
+ 3
that doesn't satisfied me
13th Jul 2018, 5:22 PM
Suraj Jha
Suraj Jha - avatar
+ 3
in python you can do .lower or .upper but I'm not sure about cpp.
13th Jul 2018, 6:17 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 2
Микола Федосєєв will you please explain how to use _stricmp !! ?
14th Jul 2018, 12:57 AM
Suraj Jha
Suraj Jha - avatar
13th Jul 2018, 8:22 PM
Bebida Roja
Bebida Roja - avatar
+ 1
what about using _stricmp? if (_stricmp("SoLoLeArN", s)==0) // statement 4;
13th Jul 2018, 10:58 PM
Микола Федосєєв
Микола Федосєєв - avatar
0
just make a copy of that string and translate all letters to lower or uppercase and then compare it
13th Jul 2018, 5:08 PM
Paul
0
I hope the following page contains enough useful info regarding _stricmp: http://c-language.com/c-tutorial/c-string/c-stricmp/
14th Jul 2018, 1:34 AM
Микола Федосєєв
Микола Федосєєв - avatar
0
Instead of ( s== "SoloLearn") we use (_stricmp(s,"SoloLearn")==0). In C++ we can create new string class and overwrite == operator using _stricmp.
14th Jul 2018, 1:41 AM
Микола Федосєєв
Микола Федосєєв - avatar