From 39c0cbcc70aff47e17107826aa76cb525c83b122 Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 9 Jul 2019 16:00:48 -0400 Subject: ch2 --- ch1/wc.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ch1/wc.c (limited to 'ch1/wc.c') diff --git a/ch1/wc.c b/ch1/wc.c new file mode 100644 index 0000000..4a5007d --- /dev/null +++ b/ch1/wc.c @@ -0,0 +1,29 @@ +#include + +#define IN 1 // inside a word +#define OUT 0 // outside a word + +/* + * Count lines, words, and characters in input. + */ +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); +} -- cgit v1.2.3