Fixed a potential bug that would cause a hang on unrecognized input. Added a 39th position to both charCount and printTable for storing, enumerating, and displaying untracked characters that do not fit to a defined position in charCount.

This commit is contained in:
Reina Harrington-Affine 2025-08-26 17:52:11 +00:00
parent 22694dfae0
commit 73d3fe21ac

View file

@ -11,16 +11,16 @@ int main(){
* 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*/
int printTable [38] = { '#' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f', 'g' , 'h' ,
int printTable [39] = { '#' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f', 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n', 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' ,
'v' , 'w' , 'x' , 'y' , 'z' , '!', '0' , '1'
, '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9'};
, '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '?'};
/*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*/
int charCount[38] = { 0 };
int charCount[39] = { 0 };
int i = 0;
@ -37,6 +37,8 @@ int main(){
++charCount[a - 64];
} else if(a >= 48 && a <= 57) { /*Check for numbers*/
++charCount[a - 20];
} else {
++charCount[38]; /*Catch-all for unrecognized/untracked input*/
}
}
/*Input text enumeration complete; set values in refTable for printing...*/
@ -50,7 +52,7 @@ int main(){
/*Print a histogram of gathered data (recycled from ex1_13_improved.c.*/
i = 0;
printf("\nHistogram of letters encountered.\n");
for (int b = charCount[i]; i <= 37; ++i){
for (int b = charCount[i]; i <= 38; ++i){
printf ("%1c ", printTable[i]);
for (; b > 0; --b){
printf("|");