-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path1-11.c
39 lines (33 loc) · 794 Bytes
/
1-11.c
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
36
37
38
39
/*
* Exercise 1-11. How would you test the word count program? What kinds of input
* are most likely to uncover bugs if there are any?
*
* By Faisal Saadatmand
*/
/*
* Answer: According to this definition of a word (characters grouped together),
* means that inputs such as hyphenated words, word's with apostrophise, etc.,
* will be counted as one word.
*/
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
int main(void)
{
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("lines: %d words: %d character: %d\n", nl, nw, nc);
return 0;
}