c-first-steps

a C playground
Index Commits Files Refs README
commit 4603e70459f1c0294b836284cfd24c94b83d26e5
parent d295ee0eadf4a43fece665981ca8234a9937d518
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Tue, 27 Oct 2020 10:53:11 -0300

lines, words, and characters counter

Diffstat:
Alwc.c | 36++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+), 0 deletions(-)
diff --git a/lwc.c b/lwc.c
@@ -0,0 +1,36 @@
+/*    count lines, words, and characters in input    */
+
+
+#include <stdio.h>
+
+#define    IN    1    /* inside a word    */
+#define    OUT    0    /* outside a word    */
+
+int main(void) {
+
+    int c, nl, nw, nc, state;
+
+    state = OUT;
+    nl = nw = nc = 0;
+
+    while((c = getchar()) != EOF) {
+        if(c != '\n') {
+            ++nc;
+        }    else {
+            ++nl;
+        }
+
+        if((c == ' ') || (c == '\n') || (c == '\t')) {
+            state = OUT;
+        }    else if(state == OUT) {
+            state = IN;
+            ++nw;
+        }
+
+
+    }
+
+    printf("lines: %d, words: %d, chars: %d\n", nl, nw, nc);
+
+    return 0;
+}