# K&R C Program Exercise 1-13

K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language, second addition, by Brian W.Keringhan and Dennis M.Ritchie(Prentice Hall,1988). You can learn and solve K&R C Programs Exercise. K and R solution to print a histogram of the lengths of words in its input. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include <stdio.h>#define MAXLN 10int main(void){ int c; int inspace = 0; long lengtharr[MAXLN + 1]; int wordlen = 0; int firstletter = 1; long value = 0; long maxval = 0; int index = 0; int done = 0; for(index = 0; index <= MAXLN; index++) {  lengtharr[index] = 0; } while(done == 0) {  c = getchar();  if(c == ' ' || c == 't' || c == 'n' || c == EOF)  {   if(inspace == 0)   {    firstletter = 0;    inspace = 1;    if(wordlen <= MAXLN)    {     if(wordlen > 0)     {      value = ++lengtharr[wordlen - 1];      if(value > maxval)      {       maxval = value;      }     }    }    else    {     value = ++lengtharr[MAXLN];     if(value > maxval)     {      maxval = value;     }    }   }   if(c == EOF)   {    done = 1;   }  }  else  {   if(inspace == 1 || firstletter == 1)   {    wordlen = 0;    firstletter = 0;    inspace = 0;   }   ++wordlen;  } } for(value = maxval; value > 0; value--) {  printf("%4d  | ", value);  for(index = 0; index <= MAXLN; index++)  {   if(lengtharr[index] >= value)   {    printf("*  ");   }   else   {    printf("   ");   }  }  printf("n"); } printf("      +"); for(index = 0; index <= MAXLN; index++) {  printf("---"); } printf("n       "); for(index = 0; index < MAXLN; index++) {  printf("%2d ", index + 1); } printf(">%dn", MAXLN); return 0;}`
`Read more Similar C ProgramsC BasicK and R C Programs Exercise`

You can easily select the code by double clicking on the code area above.

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

(you can send this program to your friend using this button)

List of C Programs
(c) www.c-program-example.com

## One comment on “K&R C Program Exercise 1-13”

• Hi…
It's really a nice doing by sharing this kind of good programming solutions to learn from it….

Here is 1 that I had written in attempt minimize the size , Hope that it would b helpful…
***************************************************

#include
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */

/* Exercise 1-13. 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.
*/

// VERTICAL ORIENTATION

int main()
{
int c,nw, nc, state;
int i,j; //Counters
int nchar[100],maxlen=0; /*for holding peak value of no. of char; For max. lenght of a word*/
state = OUT;
nw = nc = 0;
while ((c = getchar()) != EOF)
{
++nc;

if (c ==' ' || c =='n' || c =='t')
{
nc=nc-1;
if(state == IN)
{
nchar[nw]=nc;
if(nc > maxlen)
maxlen = nc;
nc=0;
}
state = OUT;
}
else if (state == OUT)
{
state = IN;
++nw;
}
}

printf("nThe Histogram of the lengths of words:n");
for(i=maxlen ; i>=0 ; i–)
{
for(j=1 ; j<= nw ; j++)
{
if(nchar[j] > i)
putchar('*');
else
putchar(' ');
}
putchar('n');
}
return 0;
}