C Language K&r Exercise series--the number of letters per word in a statistical document, output in histogram form

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.