Letter Histogram
Completed
Read a line and print, for each letter that appears (case-insensitive, alphabetical), letter count.
Input
One line.
Output
One line per letter.
Example
Input
Hello, World!
Output
d 1 e 1 h 1 l 3 o 2 r 1 w 1
Editor
C
Hints
tolower(c) - 'a' gives the letter's slot 0-25.
Solution explanation
Frequency tables are the basis of cryptanalysis and compression.
Show solution
#include <stdio.h>
#include <ctype.h>
int main() {
char s[300];
int count[26];
for (int i = 0; i < 26; i++) count[i] = 0;
fgets(s, 300, stdin);
for (int i = 0; s[i] != '\0'; i++) {
if (isalpha(s[i])) count[tolower(s[i]) - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (count[i] > 0) printf("%c %d\n", 'a' + i, count[i]);
}
return 0;
}