I am new to C++, working my way through a book and am learning some great stuff. However, I am seriously stuck on one of the exercises and I can’t figure the principle out.
The question calls for me to change all the characters in the string (“Hello World”) to the letter X using the new Range For statement.
I was succesful in getting the correct output by entering this code:
string initial ("Hello World!");
for (auto &initChange : initial)
cout << "X";
However I know this code is far from correct given that an example is to change all the text in the same string to uppercase uses the following:
string s("Hello World!!!");
for (auto &c : s)
c = toupper(c);
cout << s << endl;
I understand this code, but I cannot get a variation of it to work for the purposes of the exercise.
I have been pulling my hair out for 2 days now to figure out this simple thing. Any help would be greatly appreciated!
A simple assignment should work:
Note that
c = "X"will not work because"X"is of typechar const[2](which degenerates tochar const*) whilstcis of typechar.'X'has the appropriate type:char.