This code is supposed to count the number of occurrences of each lower-case letter of the alphabet for any input. It should ignore other inputs and stop when EOF is read. It works fine for every letter except z. If you run it you'll find that it displays an outrageous value for z occurrences.
So please, if you will, school me on my errors. I'm new to this and can't seem to figure it out.
Code:
#include<stdio.h>
int main() {
int alphabet[25] = {0};
char c = 0;
int d;
for(d = 0; d <= 25; d++) {
printf("%c\tarray[%d]\t%d\n", d + 'a', d, alphabet[d]);
}
while(1) {
c = getchar();
if(c >= 97 && c <= 122) {
printf("DEBUG entered storage\n");
printf("c = %c, %d, array %d\n", c, c, c - 'a');
alphabet[c-'a']++;
printf("c = %c, %d, array %d\n", c, c, c - 'a');
}
if(c == EOF) {
break;
}
}
printf("Lower-case letter frequencies:\n");
for(d = 0; d <= 25; d++) {
printf("%c\tarray[%d]\t%d\n", d + 'a', d, alphabet[d]);
}
return 0;
}