From dbf602e13d324c9753e670fda02957f45250f7a1 Mon Sep 17 00:00:00 2001 From: Reina Harrington-Affine Date: Sat, 16 Aug 2025 02:14:40 +0000 Subject: [PATCH] Implemented KNR C Exercise 1-12, reusing mostly modified code from my previous (optional) implementaiton of KNR C's example wordcounting program. --- KNR C/ex1_12.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 KNR C/ex1_12.c diff --git a/KNR C/ex1_12.c b/KNR C/ex1_12.c new file mode 100644 index 0000000..24ef56e --- /dev/null +++ b/KNR C/ex1_12.c @@ -0,0 +1,24 @@ +#include + +/*Rewriting the wc program from pge 21/22 of KNR C, because the book does it in a way that I think is silly, and because I can.*/ + +int nl, nw, nc, state = 0; + +#define IN 1 /*State when selected character is in a word*/ +#define OUT 0 /*State when selected character is a newline, space, or tab (outside a word)*/ + +int main(){ + + + for(int a = getchar(); a != EOF; a = getchar()){ + ++nc; + if(a == '\t' || a == ' ' || a == '\n'){ + state = OUT; + putchar('\n'); + } else { + state = IN; + putchar (a); + } + } + return 0; +}