+ 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
9 Respuestas
+ 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")
+ 3
that doesn't satisfied me
+ 3
in python you can do .lower or .upper but I'm not sure about cpp.
+ 2
Микола Федосєєв will you please explain how to use _stricmp !! ?
+ 1
what about using _stricmp?
if (_stricmp("SoLoLeArN", s)==0) // statement 4;
0
just make a copy of that string and translate all letters to lower or uppercase and then compare it
0
I hope the following page contains enough useful info regarding _stricmp:
http://c-language.com/c-tutorial/c-string/c-stricmp/
0
Instead of ( s== "SoloLearn") we use (_stricmp(s,"SoloLearn")==0).
In C++ we can create new string class and overwrite == operator using _stricmp.