I thought I'd give it a shot. What I came up with is from a culmination of my last two submissions, incorporating most of the feedback from this community, including better naming of variables, initialization, and making my program a bit more user-friendly. I'm not sure which optimizations I can make or if there are any issues with my code.
Any feedback is welcome!
#include <stdio.h>
/* print the length of words as input to a histogram with vertical bars */
int main(void) {
int c, i, j;
int word_length = 0;
int max_word_frequency = 0;
int histogram[10] = {0};
putchar('>');
while ((c = getchar()) != EOF && word_length < 10) {
if (c != ' ' && c != '\n' && c != '\t') {
++word_length;
}
else {
++histogram[word_length];
word_length = 0;
}
}
for (i = 0; i < 10; ++i) {
printf("%d ", i);
if (histogram[i] > max_word_frequency)
max_word_frequency = histogram[i];
}
putchar('\n');
for (i = 0; i < max_word_frequency; ++i) {
for (j = 0; j < 9; ++j) {
if (histogram[j] > 0) {
printf("x ");
histogram[j] = histogram[j] -1;
}
else {
printf(" ");
}
}
putchar('\n');
}
}