From d064db2b171e3da74fd5082ae29456f64caeafeb Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Fri, 12 Nov 2021 22:58:29 -0500 Subject: [PATCH] Word counting example --- 09-word-count.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 09-word-count.c diff --git a/09-word-count.c b/09-word-count.c new file mode 100644 index 0000000..e7d19bd --- /dev/null +++ b/09-word-count.c @@ -0,0 +1,25 @@ +#include + +#define IN 1 +#define OUT 0 + +/* count lines, words, and characters in input */ +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); +}