I got the program to work as expected, but can anyone explain how it works?
#include <iostream>
using namespace std;
int main(void) {
int exit;
string name;
cin >> name;
for (int i = 0; i < name.length(); i++) {
// the line below is the one I don't understand
if ('a' <= name[i] && name[i] <= 'z') name[i] = char(((int)name[i]) - 32);
}
cout << name;
cin >> exit;
return 0;
}
EDIT: Let me rephrase:
The thing I don’t understand is how does the string-to-array deal work, as in:
'a'<= name[i]. What exactly does this compare and how?
EDIT2
Thanks for the quick responses guys, love you all. I figured it out.
I assume from the edit in your comment that you are wondering how the
[]can apply to astringobject. The operator[]is overloaded forstringto return a reference to the character at the specified position offset of the represented string. There need not be any direct conversion of thestringinto an array. The code that implements the overload could well be walking a linked list. It depends on howstringwas implemented.