counting letters

need help in understanding what this code does
frequencyArray[tolower(ch - 'a')].frequency++;

it is part of a while/if statement

while (ch != '.'){
if (isalpha(ch)){
frequencyArray[tolower(ch - 'a')].frequency++;
}
cin>>ch;
}
Last edited on
Are you sure this isn't ...ay[tolower(ch) - 'a'].frequ... ?
this is a very interesting algorithm :)) this port of your code dont have any problem !! you must have array[26] of one data struct or class and indexing with tolower(ch) - 'a' :)) this is too interesting :D i really like it :D first time see such as. when indexing the array then ++ the one member of struct "frequency"
yes it is is tolower(ch)-'a'.frequency++ can someone explain what this does.

it is part of a while/if statement

while (ch != '.'){
if (isalpha(ch)){
frequencyArray[tolower(ch) - 'a'].frequency++;
}
cin>>ch;
}
Last edited on
You are iterating through a string. Lets say the string is "An."
First 'A' is found. tolower('A') returns 'a'. Then you subtract 'a' from it (which makes sense, because characters are their ascii codes. 'a' == 97, I think ) and get a 0. Then you find the 0th element of frequencyArray and increment one of it's members.
Then you find 'n'. tolower('n') returns 'n'. 'n'-'a' = 13 (I think). Then the 13th element of frequencyArray is found and incremented.
Finaly a '.' is found and the loop terminates.
Thank you for the explination hamsterman i know get what it does.
Topic archived. No new replies allowed.