+ 1

Convert a string to uppercase or lower case in C++.

In one video, I saw that to convert a string to uppercase(with the data type char) you can use the function strupr(), or strlwr() (to lowercase) but!... I found in the internet that these both functions aren't standard (because of that, is not recommended to use it). so... how can I convert a string (char or string data type) to uppercase or lowercase without using those non standard functions? I found that you can use toupper() or tolower(), but it only works with char data type. I want to use string too.

6th Aug 2018, 12:17 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
5 odpowiedzi
+ 3
Eduardo Perez Regin there is a toupper in global namespace also... you can use it as follow to convert string directly to upper case: #include <iostream> #include <algorithm> using namespace std; int main() { string str = "asd"; transform(str.begin(),str.end(),str.begin (),::toupper); cout << str << endl; return 0; }
6th Aug 2018, 1:04 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
with toupper() and tolower() you can use a for loop to go threw your string and make them all uppercase or lowercase.
6th Aug 2018, 12:39 AM
Mooaholic
Mooaholic - avatar
0
Mooaholic but only works with char data type right? what about if my string is a string data type? in that case you cannot use toupper() and tolower(), right? when you are receiving a string, should I use char data type or string data type?
6th Aug 2018, 12:48 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
0
you can use a forloop so if you have string a = “test”; and your want to make that uppercase you can use for(int i = 0; i < a.size(); i++){ a[i] = toupper(a[i]); } this will go threw each char of the string test and make it TEST.
6th Aug 2018, 12:55 AM
Mooaholic
Mooaholic - avatar
0
thx to everyone!!
7th Aug 2018, 10:02 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar