# Thread: Exercise 1-13 in K&M

1. ## Exercise 1-13 in K&M

Hey guys. Have been self teaching myself programming and got stuck on another problem. For problem 1-13 in the K&M, it asks for a program that makes a histogram of lengths of words. I figured that was a too too difficult and decided instead to just make a program that counts the numbers of words of different lengths, and to make it even easier I decided to just limit it to 10. Yet I can't even get that to work =\

Code:
```#include <stdio.h>

main(){

#define IN 1
#define OUT 0

int c, i, j, state;
int word[10];

i = 0;
state = OUT;

for (j=0;j<=9;++j)
word[j]=0;

while ((c = getchar()) != EOF)
{
if ((c == ' '|| c == '\b'|| c == '\n') && state == IN )
{
state = OUT;
++word[i - '1'];
i = 0;
}
else if ((c==' '|| c=='\b'|| c=='\n') && state == OUT);

else
{
++i;
state == IN;
}
}

printf("The number of words length 1 2 3 4 5 6 7 8 9 10 is\n\t\t\t\t");

for (j=0;j<=9;++j)
printf("%d ", word[j]);
}```
when I print it just prints out all 0's. Not quite sure what I'm doing wrong. Thanks in advance.

2. It's mostly okay -- you just have a couple of mistakes. The logic of the code, which is the difficult bit, looks to be fine.

Code:
```else
{
++i;
state == IN;
}```
Looks like a typo I think you wanted '=' (assignment) not '==' (equality).

Code:
`    ++word[i - '1'];`
'1' in single quotes is the character "1", not the number 1. This line will subtract the character encoding for 1 from i!

Finally,

Code:
`    else if ((c==' '|| c=='\b'|| c=='\n') && state == OUT);`
Nothing wrong with this, it's legal C and will work, but I don't really like the style. I know it's exactly the same but I'd write:
Code:
```    else if ((c==' '|| c=='\b'|| c=='\n') && state == OUT)
; // do nothing```

3. Ah. Yeah that got it. Thanks a bunch smokeyangel