Name: Anonymous 2008-12-14 18:01
Write a program to print a histogram of the lengths of words in its input. It is
easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
The horizontal version:
:/ ?
easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
The horizontal version:
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAX_LEN 10
int main() {
int c, i, j, state;
long length = 0, wlength[MAX_LEN];
for (i = 0; i < MAX_LEN; ++i)
wlength[i] = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\t' || c == '\n') {
if (length < MAX_LEN && state == IN)
++wlength[length];
else if (length >= MAX_LEN && state == IN)
++wlength[MAX_LEN - 1];
++wlength[0];
length = 0;
state = OUT;
}
else {
++length;
state = IN;
}
}
if(state == IN)
++wlength[length];
printf("\nno. of:\n");
for (i = 0; i < MAX_LEN; ++i) {
if(i == 0)
printf("wspace chars: ");
else
printf("%i-char words: ", i);
for (j = 1; j <= wlength[i]; ++j)
printf("#");
printf("\n");
}
return 0;
}:/ ?