1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input
*
* I would focus on testing the state transitions, since character and
* line counting are straightforward. The character count increments for
* every character that is not EOF, and the line count increments whenever
* a newline is encountered. Word counting is more complex because it
* depends on correctly detecting boundaries. I would include test cases
* with varying numbers of spaces, tabs, and newlines to verify that word
* boundaries are handled correctly, since those inputs are the most likely
* to reveal bugs.
*/
int main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
nc++;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
|