leonard said:
I want to know the correct way to answer exercise 1-11 of K&R.
The only bug that I can find is that nw counts one to many words.
(if there are 8 words, nw will be 9) Am I correct aor is there
more to it
leonard
This answer was taken from The C answer book by
Clovis L Tondo & Scott E Gimpel
which gives all the answers to K&R
To test the word count program first try no input,
The output should be: 0 0 0 (zero newlines, zero words, zero characters).
Then try a one-character word. The output should be: 1 1 2.
(one new line,one word, two characters-a letter followed by
a new line character). Then try a two character word.
the output should be: 1 1 3 (one new line, one word,
three chacters- two chacters followed by a new line character).
In addition, try 2 one-chacter words (the output should be 1 2 4) and
2 one-character words-one word per line(the output should be 2 2 4).
The kinds of input most likely to uncover bugs are those that test boudary
conditions. Some boundaries are:
-no input
-no words-just new lines
-no words-just blanks, tabs, and newlines
-one word per a line-no blanks and tabs
-word starting at the beginning of the line
-word starting after some blanks