c++stringfor-loopchartoupper

C++ "toupper" not converting character to uppercase


I'm trying to convert every letter in a string to uppercase. I'm looping through each character and using toupper on it. However, when I print the new string out, it's not working. Sorry if this is a newbie question. Any help would be greatly appreciated :)

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    string str1, str2;
    cin >> str1 >> str2;
    int len = str1.size();

    for (int i = 0; i < len; i++) {
        toupper(str1[i]);
        toupper(str2[i]);
        cout << str1[i] << " " << str2[i] << endl;
    }
}

Solution

  • This may be better, depending on your coding standards:

    std::transform(str1.begin(), str1.end(), str1.begin(), std::toupper);
    std::transform(str2.begin(), str2.end(), str2.begin(), std::toupper);
    

    The above uses the STL function transform to convert the string to all uppercase.