#include #include #include #include #define max_word 100 #define max(a, b) ((a) > (b) ? (a) : (b)) 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+1]={0}; state s = out; uint32_t cl = 0; while(true) { int c = fgetc(input); if (is_word(c)){ if (s == out) { s = in; cl=1; } else { cl++; } } else { if (s == in) { s = out; if (cl > max_word) { printf("cl=%d\n", cl); fprintf(output_err, "word too long\n"); fflush(output_err); return 1; } lens[cl]++; } else { // do nothing I guess } } if (c == EOF) { break; } } // Do 2 passes to find the longest word length with some data in it uint16_t mx = 0; for(uint16_t i=1; i<=max_word; i ++) mx = max(mx, lens[i]>0 ? i : 0); for(uint16_t i=1; i<=mx; i ++) { fprintf(output, "%2d: ",i); for(uint16_t j=0; jbuffer); return 0; } #endif