/*
* Exercise 1-13
* 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.
*
* fduan, Dec. 10, 2011
*/
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAX_LEN 10
int main()
{
int c, i, j, len, max_len, state;
int len_word[MAX_LEN + 1] = { 0 };
max_len = len = 0;
state = OUT;
while( ( c = getchar() ) != EOF )
{
if( c != ' ' && c != '\t' && c != '\n' )
{
state = IN;
++len;
}
else if( state == IN )
{
state = OUT;
++len_word[len];
max_len = ( len > max_len ) ? len : max_len;
len = 0;
}
}
for( i = 1; i <= max_len; ++i )
{
printf( "%2d:\t", i );
for( j = 0; j < len_word[i]; ++j )
putchar( '*' );
putchar( '\n' );
}
return 0;
}
K&R C Exercise 1-13 Solution
最新推荐文章于 2024-07-30 10:50:46 发布