From 5506047efd5cde023e793b437ede1328054a0890 Mon Sep 17 00:00:00 2001 From: "martin@ashbysoft.com" Date: Sun, 12 Jun 2022 17:41:24 +0000 Subject: ex1-13 --- ex1-13.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ex1-13.c diff --git a/ex1-13.c b/ex1-13.c new file mode 100644 index 0000000..87fe0b0 --- /dev/null +++ b/ex1-13.c @@ -0,0 +1,64 @@ +#include +#include +#include + +#define max_word 100 + +typedef enum _state {in,out} state; +bool is_word(char c) { + return c != ' ' + && c != '\t' + && c != '\n'; +} +/* print a histogram of word lengths + read from input + print histo to output + print errors warnings to output_err */ +int whisto(FILE* input, FILE* output, FILE* output_err) { + uint32_t lens[max_word]={0}; + int c = fgetc(input); + if (c == EOF) { + fprintf(output_err, "warning: no input!\n"); + goto spout; + } + state s = is_word(c) ? in : out; + uint32_t cl = s == in ? 1 : 0; + while ((c = fgetc(stdin))!= EOF){ + if (is_word(c)){ + if (s == in) { + cl++; + } else { + s = in; + cl=1; + } + } else { + if (s == in) { + s = out; + lens[cl]++; + } else { + // do nothing I guess + } + } + } + // need to count final word + if (s == in) { + lens[cl]++; + } +spout: + for(uint16_t i=1; + i