Original question:
Write a program to print ahistogram of the lengths of words on its input. It's easy-to-draw thehistogram with the bars horizontal; A vertical orientation is more challenging.
This is my first hundred lines of code (with comments, spaces, etc.)
Main two parts: input and output
#include < stdio.h > #define MAXWORDLEN 10main ( void ) {int C;int wordlen = 0;int thisidx = 0;long lengtharray[maxwordlen + 1 ];long thisval = 0;long maxval = 0;//initializeint inspace = 0; int firstletter = 1;int done = 0;for ( thisIdx = 0; thisidx <= maxwordlen; thisidx++ ) {lengtharray[thisidx] = 0;} while ( done == 0 ) {C = getchar ();if ( c == ' ') | | c == ' \ n ' | | c == ' t ' | | c == EOF ) {if ( inspace == 0 ) {Inspace = 1;firstletter = 0;if ( wordLen <= MAXWORDLEN ) {Thisval = ++lengtharray[wordlen - 1];if ( thisVal > maxVal ) {maxval = thisval;}} Else{thisval= ++lengtharray[maxwordlen];if ( thisVal > maxVal ) {maxval = Thisval;}}} if ( c== EOF ) {done = 1;}} else{if ( inspace == 1 | | firstLetter == 1 ) {wordlen= 0;inspace= 0;firstletter = 0;} ++wordlen;}}
Then the output section
for ( thisVal = maxVal; thisVal > 0; thisVal-- ) { printf ( "%4d |", thisval ); for ( thisidx = 0; thisidx <= maxwordlen; thisidx++ ) { if ( lengtharray[ thisidx] >= thisval ) { printf ( " * " "; } else { printf ( ); } } printf ( "\ n" ); } printf ( " |_ " ) &NBsp; for ( thisIdx = 0; thisIdx <= maxwordlen; thisidx++) { printf ( "___" ); } printf ( "\n " ); for ( thisIdx = 0; thisIdx < MAXWORDLEN; thisidx++ ) { printf ( "%3d", thisidx + 1 ); } printf ( " >\n" ); for ( thisIdx = 0; thisIdx < MAXWORDLEN + 2; thisIdx++ ) { printf ( " " ); } printf ( " %2d\n", maxwordlen );}
Post-Run tests:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/43/wKioL1UEMPbB9PFKAAOBbmCEAcs586.jpg "title=" 1_13. JPG "alt=" Wkiol1uempbb9pfkaaobbmceacs586.jpg "/>
C Language K&r Exercise series--the number of letters per word in a statistical document, output in histogram form