K&R C Exercise 1-13 Solution

来源:互联网 发布:九宫格锁屏软件大全 编辑:程序博客网 时间:2024/05/06 20:44
/* * 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 IN1#define OUT0#define MAX_LEN10int 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;}

原创粉丝点击