blob: 91cfc75e3999c45ff4a72be33fe953dd173019df (
plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <stdio.h>
#define IN 1 /* inside of a word */
#define OUT 0 /* outside of a word */
/* count lines, words, and characters */
void
wc()
{
/*
* c must be big enough to hold any value getchar() returns so we use
* int instead of char to hold EOF, an integer defined in stdio
*/
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n') {
++nl;
}
/* && is higher precedence than || */
if (c == ' ' || c == '\t' || c == '\n') {
state = OUT;
} else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
/* print input separated by new lines */
void
print_word_line()
{
int c;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\t' || c == '\n') {
putchar('\n');
} else {
putchar(c);
}
}
}
int
main()
{
wc();
/* print_word_line(); */
return 0;
}
|