#define IN 1 /*State when selected character is within a word*/
#define OUT 0 /*State when selected character is any kind of whitespace*/
intmain(){
intcharCount[10];/*Array for storing char lengths*/
intlast,lCount,state,i;/*last stores last character, or 0 at init. lCount increments every time the main for loop encounters a non-newline/whitespace character and then resets when one is encountered after commiting its value to charCount.*/
/*Check if current char is WS. If so, set state to OUT and check if the last character was not. If it was not, then we are exiting a word, and so comitting lCount to charCount.*/
if(lCount>10){/*Array charCount only stores values up to 10, so we will add any longer words to the column for 10+*/
++charCount[9];/*Adding at position 11 in the array, since it starts at 0.*/
}else{
++charCount[(lCount-1)];/*This decrements to array position lCount-1 in order to use position 0 in the array as position 1 since there are no 0-letter words, thus saving 4 bytes in the stack by not needing an array 11 ints long*/
}
lCount=0;/*Reset lCount when a condition that dumps it to charCount is triggered*/
printf("\nHistogram of char counts by frequency of words enountered with a given count.\n");
i=0;/*Reset value of i, since we're reusing it*/
for(intb=charCount[i];i<10;++i){
if(i<9){
printf("%d ",(i+1));
}elseif(i=9){
printf("%3s","10+");
}
/*For loop with no entry condition that prints a | and then decrements b. TODO: Figure out a better way to do this that doesn't require cycling this loop b number of times.*/
for(b>0;b>0;--b){
printf("|");
}
printf("\n");
b=charCount[(i+1)];/*Get the value of the next position in the array; since i hasn't incremented yet, it's i+1*/