Ask questions when confronted by code you don't understand instead of complaining "it doesn't work!"
Unless you tell us we don't know what you do or don't know. We are not mind readers, we don't have a crystal ball.
Copy and pasting blindly code you don't understand can be quite frustrating, as you know.
Don't understand the for loop?
Do you understand what the std::string size() function does? It returns the number of characters contained in the string.
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
#include <cctype>
#include <string>
int main()
{
std::string str1 = "One";
std::string str47 = "Forty-seven";
std::cout << str1 << ", " << str1.size() << '\n'
<< str47 << ", " << str47.size() << '\n';
}
|
Do you understand arrays? How for loops can be used to access each element? C++ strings are (VERY simplistic explanation) C char array strings, plus a lot of functions to make working with std::string easier.
The for loops walk through the string, from 0 to the string's size(), using std::toupper or std::tolower to change each individual character in the string.
I could have written more compact for loops for the string conversions that really would have been confusing to someone new to C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void toUpper(std::string& str)
{
for (auto& itr : str)
{
itr = std::toupper(itr);
}
}
void toLower(std::string& str)
{
for (auto& itr : str )
{
itr = std::tolower(itr);
}
}
|