From 83e0861e200999ed46a1a1a8afa0b5638e48e14b Mon Sep 17 00:00:00 2001 From: "martin@ashbysoft.com" Date: Sat, 11 Jun 2022 21:25:01 +0000 Subject: ex1-9 ex1-10 --- ex1-10.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ex1-10.c (limited to 'ex1-10.c') diff --git a/ex1-10.c b/ex1-10.c new file mode 100644 index 0000000..0b0c018 --- /dev/null +++ b/ex1-10.c @@ -0,0 +1,36 @@ +#include +#include +#include + +/* Track whether we are inside or outside a word */ +typedef enum _state {IN,OUT} state; + +/* true if c is a 'word' character (not a blank, space, or a tab) */ +bool is_word(char c) { + return c != ' ' && c != '\t' && c != '\n'; +} + +/* Counts characters, lines, and words from stdin. + + 'characters' are single byte characters. No handling for encodings in c standard lib. + 'lines' are newline characters. + 'words' are contiguous sequences of characters other than space, tab, newline */ +int main(void) { + int c; + state s = OUT; + uint32_t cc = 0, lc = 0, wc = 0; + while ((c = getchar()) != EOF) { + cc++; + if (c == '\n') { + lc++; + } + if (!is_word(c)) { + s = OUT; + } else if (s == OUT) { + s = IN; + wc++; + } + } + printf("cc=%d lc=%d wc=%d\n", cc, lc, wc); + return 0; +} \ No newline at end of file -- cgit v1.2.3-ZIG