From 313e4aaa3b42523a0ab7ed19d5ce95d27e375721 Mon Sep 17 00:00:00 2001 From: Reina Harrington-Affine Date: Sat, 16 Aug 2025 01:53:43 +0000 Subject: [PATCH] Added ex1_10-5.c, an implementation of an example wordcount program on pages 21/22 of KNR C, purely as an exercise to see if I could do it more efficiently than the book. This resulted in a binary that was, 138 lines long as disassembled. --- KNR C/ex1_10-5.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 KNR C/ex1_10-5.c diff --git a/KNR C/ex1_10-5.c b/KNR C/ex1_10-5.c new file mode 100644 index 0000000..94c0272 --- /dev/null +++ b/KNR C/ex1_10-5.c @@ -0,0 +1,26 @@ +#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; + if (a == '\n') + ++nl; + } else { + state = IN; + ++nw; + } + } + printf("%d %d %d\n", nl, nw, nc); + return 0; +}