Hi guys, I've to read a txt file and produce a statistic of each alphabetic char occurrence.
I'm approaching this task reading char by char until EOF and using an int array to hold the number of occurrences of each character read so far!
So that's my solution:
int notalpha=0;
int nchar[26];
for (...) char[i] = 0; //set each char to 0
while (c ...){
if(toupper(c) == 'A') nchar[0]++;
else if (toupper(c) == 'B') nchar[1]++;
else if ....
else if (toupper(c) == 'Z') nchar[25]++;
else notalpha++;
}
printf("A = %d, B = %d, ...", char[0], char[1] ...);
Obviously I'm confident it can be largely improved, since it's very bad style, but I don't see the light..
Is it possible to avoid the 26 rows if-else, and the horrible printf?