2025-08-26 17:42:41 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
/*Initial implementation of KNR C Exercise 1-14*/
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
2025-09-05 18:49:44 +00:00
|
|
|
/*Initialize an array of labels for the numerical table later. Done at
|
2025-08-26 17:42:41 +00:00
|
|
|
* copile rather than procedurally in order to avoid adding an extra if
|
|
|
|
|
* statement on the print loop to look for the specific case where i is
|
|
|
|
|
* 0 or 27 and print a different character that isn't in refTable*/
|
|
|
|
|
/*TODO find a way to do this so that # and ! are replaced by WSPC and PCTN
|
|
|
|
|
* respectively*/
|
2025-08-26 17:52:11 +00:00
|
|
|
int printTable [39] = { '#' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f', 'g' , 'h' ,
|
2025-08-26 17:42:41 +00:00
|
|
|
'i' , 'j' , 'k' , 'l' , 'm' , 'n', 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' ,
|
|
|
|
|
'v' , 'w' , 'x' , 'y' , 'z' , '!', '0' , '1'
|
2025-08-26 17:52:11 +00:00
|
|
|
, '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '?'};
|
2025-08-26 17:42:41 +00:00
|
|
|
|
|
|
|
|
/*Initialize character count storage array. Whitespaces are stored at pos 0
|
|
|
|
|
* while letters are stored at their numeric positions in the alphabet, and
|
|
|
|
|
* punchtuation is stored at position 27.Numbers are stored at positions
|
|
|
|
|
* 28 through 38*/
|
2025-08-26 17:52:11 +00:00
|
|
|
int charCount[39] = { 0 };
|
2025-08-26 17:42:41 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*Begin input text enumeration loop...*/
|
|
|
|
|
for(int a = getchar(); a != EOF; a = getchar()){
|
|
|
|
|
/*Begin whitespace filter check*/
|
|
|
|
|
if(a == '\n' || a == ' ' || a == '\t'){
|
|
|
|
|
++ charCount[0];
|
|
|
|
|
continue; /*Escape and restart loop to avoid running redundant
|
|
|
|
|
tests.*/
|
|
|
|
|
} else if(a >= 97 && a <= 122) { /*Check for lowercase letters*/
|
|
|
|
|
++charCount[a - 96];
|
|
|
|
|
} else if(a >= 65 && a <= 90) { /*Check for uppercase letters*/
|
|
|
|
|
++charCount[a - 64];
|
|
|
|
|
} else if(a >= 48 && a <= 57) { /*Check for numbers*/
|
|
|
|
|
++charCount[a - 20];
|
2025-08-26 17:52:11 +00:00
|
|
|
} else {
|
|
|
|
|
++charCount[38]; /*Catch-all for unrecognized/untracked input*/
|
2025-08-26 17:42:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*Input text enumeration complete; set values in refTable for printing...*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*Begin output generation...*/
|
|
|
|
|
/*Vertical table of numerical values:*/
|
|
|
|
|
for(int i=0; i <= 27; ++i)
|
|
|
|
|
printf("%4c %4d\n", printTable[i], charCount[i]);
|
|
|
|
|
|
|
|
|
|
/*Print a histogram of gathered data (recycled from ex1_13_improved.c.*/
|
|
|
|
|
i = 0;
|
|
|
|
|
printf("\nHistogram of letters encountered.\n");
|
2025-08-26 17:52:11 +00:00
|
|
|
for (int b = charCount[i]; i <= 38; ++i){
|
2025-08-26 17:42:41 +00:00
|
|
|
printf ("%1c ", printTable[i]);
|
|
|
|
|
for (; b > 0; --b){
|
|
|
|
|
printf("|");
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
b = charCount[(i + 1)];
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 00:13:13 +00:00
|
|
|
return 0;
|
2025-08-26 17:42:41 +00:00
|
|
|
}
|