| 1 |
#include <stdio.h> |
| 2 |
|
| 3 |
#define IN 1 /* inside a word */ |
| 4 |
#define OUT 0 /* outside a word */ |
| 5 |
|
| 6 |
/* count lines, words, and characters in input */ |
| 7 |
int main(int argc, char *argv[]) { |
| 8 |
int c, nl, nw, nc, state; |
| 9 |
FILE *fp; |
| 10 |
char *infile = "../../src/bib"; |
| 11 |
if (argc > 1) |
| 12 |
infile = argv[1]; |
| 13 |
fp = fopen(infile, "r"); |
| 14 |
if (fp == NULL) { |
| 15 |
fprintf(stderr, "can't open %s\n", infile); |
| 16 |
exit(1); |
| 17 |
} |
| 18 |
|
| 19 |
state = OUT; |
| 20 |
nl = nw = nc = 0; |
| 21 |
while ((c = getc(fp)) != EOF) { |
| 22 |
++nc; |
| 23 |
if (c == '\n') |
| 24 |
++nl; |
| 25 |
if (c == ' ' || c == '\n' || c == '\t') |
| 26 |
state = OUT; |
| 27 |
else if (state == OUT) { |
| 28 |
state = IN; |
| 29 |
++nw; |
| 30 |
} |
| 31 |
} |
| 32 |
printf("%d %d %d\n", nl, nw, nc); |
| 33 |
return 0; |
| 34 |
} |