#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N 100
int main()
{
int count[26] = {0};
int i;
char ch ='a';
printf("Enter a sentence (end by '.'): ");
while (ch != '.') {
ch = getchar();
count[(tolower(ch) - 'a')]++;
for (i = 0; i < 26; i++) {
printf("'%c' has %2d occurrences.\n", i + 'a', count[i]);
}
}
return 0;
}
The program does what it is supposed to do. The counter works fine the issue is that the program runs though every single letter twice and prints out which letters occurs 0 times. The correct output should be as follows:
Enter a sentence end by '.'
The scanf reads This.
Correct Output:
T occurs 1 times
H occurs 1 times
I occurs 1 times
S occurs 1 times
But the output from the code goes through every single letter and also prints out which letters did not occur at all. I need to get rid of the letters that occur "0 times" and only display letters that occur 1 or more times.
Any help would be appreciated!