Prof give sample code, but do not understand what's going on up in here:
Entire code at bottom;
questions in the comments within code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int lettercount [LETTERS];
for(int i = 0; i < LETTERS; i++)
lettercount[i] = 0;
lettercount[tolower(str.at(0)) - tolower('a')]++; //
// what is going on with the str.at(0) - tolower('a') | why would
// or could you substract a letter from a string at the beginning?
for(int i = 0; i < LETTERS; i++)
cout << lettercount[i] << " "
<< static_cast<char>(i + tolower('a')) << endl; //
// he does here again. he adds a tolower('a') to i for some reason.
// also, what is the static_cast<char> before this? it looks like a function,
// but what is the < > ? it reads a type?
First question, this is just mapping each letter to an index in the lettercount array so 'a' is mapped to 0, 'b' to 1 and so on.
The way it maps the letters to an index is by using ascii codes because every letter is represented by a number, google ascii table to see it and better understand this.
'a' has a code (value) and 'b' has 'a' value + 1 and so on to 'z'
The same goes for the second question this time he is mapping indecies to letters so 0 becomes 'a' and so on, all what static_cast in this code does is that it converts the given number to a char according to ascii table.