is there a function that will convert all the letters in a string to upper or lower case to make it easier to make a check like "if (a=="JUMP")".
i've tryed toupper().
is there a function that will convert all the letters in a string to upper or lower case to make it easier to make a check like "if (a=="JUMP")".
i've tryed toupper().
Style is overrated.
- —₽‚¢‰Î -
Do you mean an std::string or a char[].
If the first, I think you could do
If the second, you can't compare it like that. use strcmp() like soCode:std::string a("JUMP"), b("jump"); //not sure about the capitalization of std::string::toupper() if(a == b.toupper()){ //do whatever }
The `!' is there because strcmp will return 0 is the strings are equal, -1 if the 1st is "bigger", and 1 if the 2nd is "bigger".Code:char a[5] = "JUMP", b[5] = "jump"; toupper(b); if(!strcmp(a, b)){ //code }
SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
I say what I say, I mean what I mean.
IDE: emacs + make + gcc and proud of it.
"toupper()" is not a member of basic_string. The C function toupper() operates on a single character.
The easiest thing to do is a case insensitive comparison with stricmp(), if it's available in your CRT library (it's a non-standard function).
gg
>is there a function that will convert all the letters in a string to upper or lower case
Combine toupper or tolower with the transform template function in <algorithm>:
Code:#include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; int main() { string s ( "test" ); cout<< s <<endl; transform ( s.begin(), s.end(), s.begin(), toupper ); cout<< s <<endl; }
My best code is written with the delete key.