c++ - Beginner: tolower in a vector -
so have code counting different characters in textfile , dont quite understand bottom codeline in following section do:
string linje; int nletters = 'z' - 'a' +1; vector<int> bokstaver(nletters, 0); int antalltegn = 0; while(getline(inputfile, linje)){ for(char tegn:linje){ if(isletter(tegn)){ antalltegn++; bokstaver[tolower(tegn)-'a']++; i know converts tegn variable lower case dont understand why have subtract 'a'.
characters represented integers in computers. each integer value represent character (this gets more complicated unicode, that's beyond scope of question). so, 'a' has numerical value, tolower(tegn).
numbers can thought line of numbers, value position of number on line. similarly, number-encoded characters can thought of characters in line numerical value position.
a number line:
0 1 2 3 4 a character line:
, . - b c d subtraction of 2 numbers analogous distance on number line. similarly, subtracting 2 characters distance on character line.
so, bokstaver array indices positions on character line, offset position of character 'a'.
Comments
Post a Comment