2025-08-19 17:45:32 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2025-08-22 18:23:16 +00:00
|
|
|
/*Slight modification to ex1_13.c that makes charCount an array of ints 11
|
|
|
|
|
* entries long, in order to circumvent the need for additional mathematical
|
|
|
|
|
* operations down the line as a consequence of ocurrences of the value "1"
|
|
|
|
|
* being store at position 0. This sacrifices 4 bytes of memory in the stack
|
|
|
|
|
* by extending the array, but results a shorter binary.
|
2025-08-19 17:45:32 -07:00
|
|
|
*
|
|
|
|
|
* how much shorter, you ask?
|
|
|
|
|
*
|
|
|
|
|
* 1 instruction.
|
|
|
|
|
*
|
|
|
|
|
* lol.*/
|
|
|
|
|
|
|
|
|
|
#define IN 1 /*State when selected character is within a word*/
|
|
|
|
|
#define OUT 0 /*State when selected character is any kind of whitespace*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
|
|
|
|
int charCount[11]; /*Array for storing char lengths*/
|
|
|
|
|
|
2025-08-22 18:23:16 +00:00
|
|
|
int last, 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.*/
|
2025-08-19 17:45:32 -07:00
|
|
|
/*Begin array initialization*/
|
|
|
|
|
for(i = 0; i < 11; ++i){
|
|
|
|
|
charCount[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
/*End array init*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
last = lCount = 0;
|
|
|
|
|
|
|
|
|
|
/*Begin input letter count enumeration.*/
|
|
|
|
|
for(int a = getchar(); a != EOF; a = getchar()){
|
|
|
|
|
|
2025-08-22 18:23:16 +00:00
|
|
|
/*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.*/
|
2025-08-19 17:45:32 -07:00
|
|
|
if(a == '\t' || a == ' ' || a == '\n'){
|
|
|
|
|
state = OUT;
|
2025-08-22 18:23:16 +00:00
|
|
|
if (last != '\t' && last != ' ' && last != '\n'){ /*True on word
|
|
|
|
|
exit only.*/
|
|
|
|
|
if(lCount > 10){ /*Array charCount only stores values up to 10,
|
|
|
|
|
so we will add any longer words to the
|
|
|
|
|
column for 10+*/
|
2025-08-19 17:45:32 -07:00
|
|
|
++charCount[10];
|
|
|
|
|
} else {
|
|
|
|
|
++charCount[(lCount)]; }
|
2025-08-22 18:23:16 +00:00
|
|
|
lCount = 0; /*Reset lCount when a condition that dumps it to
|
|
|
|
|
charCount is triggered*/
|
2025-08-19 17:45:32 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
state = IN;
|
2025-08-22 18:23:16 +00:00
|
|
|
++lCount; /*Increment lCount when encountering a non-WS
|
|
|
|
|
character.*/
|
|
|
|
|
}
|
2025-08-19 17:45:32 -07:00
|
|
|
last = a;
|
|
|
|
|
}
|
|
|
|
|
/*Input letter count enumeration complete*/
|
|
|
|
|
|
2025-08-22 18:23:16 +00:00
|
|
|
/*Print a table of the number of words encountered for
|
|
|
|
|
* each number of characters.*/
|
2025-08-19 17:45:32 -07:00
|
|
|
printf("Word letter count composition:\n");
|
|
|
|
|
for (i = 1; i <= 9; ++i)
|
|
|
|
|
printf("%3d", (i));
|
|
|
|
|
printf ("%3s", " 10+\n");
|
|
|
|
|
for (i = 1; i <= 10; ++i)
|
|
|
|
|
printf("%3d", charCount[i]);
|
|
|
|
|
|
|
|
|
|
/*Print a histogram of gathered data.*/
|
|
|
|
|
printf("\nHistogram of char counts by frequency of words enountered with a given count.\n");
|
|
|
|
|
i = 1; /*Reset value of i, since we're reusing it*/
|
|
|
|
|
for (int b = charCount[i]; i <= 10; ++i){
|
|
|
|
|
if (i < 10){
|
|
|
|
|
printf("%d ", (i));
|
|
|
|
|
} else if (i == 10) {
|
|
|
|
|
printf("%3s", "10+");
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 18:23:16 +00:00
|
|
|
for (; b > 0; --b){
|
2025-08-19 17:45:32 -07:00
|
|
|
printf("|");
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
2025-08-22 18:23:16 +00:00
|
|
|
b = charCount[(i + 1)]; /*Get the value of the next position in the
|
|
|
|
|
array; since i hasn't incremented yet,
|
|
|
|
|
it's i+1*/
|
2025-08-19 17:45:32 -07:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|