Hi everyone, im jabing trouble understanding how this piece of code does what it does.
Prior to this, the string spelling is inputted by the user. In the example, the user enters "-" symbols in between letter characters. The program then outputs "you spelled 'word'" without any of the symbols in between. Can someone explain to me what i am not understanding? I feel it has something to do with the role of the [] brackets. Thanks for the help!
1 2 3 4 5
string word;
int i = 0;
while(i < spelling.length())
{ word += spelling[i];
i += 2; }
spelling[i] gives you the (i+1)th character of spelling (remember, indices start at 0).
So what the code does is visit every other character of spelling and appends it onto word.
(The i += 2; line increments the position by 2.)
This could also be done by using std::remove in the <algorithm> header to remove the '-' characters (unless the word itself has dashes in it, in which case those will be removed as well).