I have having some trouble printing a "char" from an ASCII value stored in an array.
The goal overall is to read a string of input from the user, store that in an array. Then use another array to count the number of times each character appears in the string, storing the number of occurances in the array address that matches the ASCII code of the character being counted. Then to output that number of occurances along with which character was counted, and the percentage of characters.
I can successfully make the array of character occurances. And as a test I can do the counting and percentage calculations during the occurance count. But later when I try to make a FOR loop with a IF statement that ignores NULL values and prints the array address as the ASCII value for the character, the number of times the character occured, and the percentage that character appears of the total characters. I just get a NULL character printed. Below you find what I have written so far.
This is for help with a school assignment, and I am NOT asking for the solution in code. I've been trying to find out where I went wrong, and I'm not sure why one of my FOR loops works and the second one doesn't. And would appreciate some help in finding where I've gone wrong.
#include <stdio.h>
#include <stdlib.h>
#define TERMINAL -9
int main()
{
float totalCharacters;
float currentCharacterCount;
int iteration, currentInputCharacter;
char checkCharacter, input [80];
int alphabetArray[128] = {0};
totalCharacters = 0;
iteration = 0;
currentCharacterCount = 0;
currentInputCharacter = 0;
alphabetArray[128] = 0;
printf("Enter a line of test: ");
gets(input);
printf ("FREQUENCY TABLE\n");
printf ("---------------\n");
printf ("Char Count %% of Total\n");
printf ("---- ----- ----------\n");
totalCharacters = strlen(input);
printf (" ALL %5d %9.2f%%\n", strlen(input), (totalCharacters / strlen(input)) * 100);
for ( iteration = 0; input[iteration] != '\0'; iteration++)
{
checkCharacter = input[iteration];
alphabetArray[checkCharacter]++;
printf ("%4c %5d %9.2f%%\n", checkCharacter, alphabetArray[checkCharacter], ((float)alphabetArray[checkCharacter] / strlen(input)) * 100);
}
for ( iteration = 0; alphabetArray[iteration] != '\0'; iteration++)
{
printf ("%1c ", alphabetArray[iteration]);
if (alphabetArray[iteration] != '\0')
{
checkCharacter = alphabetArray[iteration];
printf ("%4c %5d %9.2f%%\n", checkCharacter, alphabetArray[checkCharacter], ((float)alphabetArray[checkCharacter] / strlen(input)) * 100);
}
}
system ("pause");
exit(0);
}