Hello, I am new to c and up until now i have understood it pretty well. I am having a difficulty writing a program that lists the characters in a phrase entered into the program. I think my problem is with the loops and maybe some variable declarations. None of the variables come out counted right, and instead of just listing the ones used in the phrase it lists all of them with wrong counts for those also. I also need to calculate percentages but once i fix my other problems i am sure i can fix it.
here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char TC; /* Character variable to count text and load ASCII array */
int TL = 0; /* Counters the number of characters in the entered text line */
int AA[128] = {0}; /* Array which stores the frequency of the the characters in
the entered text line that corresponds with its ASCII value.*/
printf("Enter a line of text: ");
while (TC = getchar() != '\n')
{
TL++; /* Counts each character */
AA[TC]++; /* Increments array element that corresponds with the character's
ASCII value */
TC = getchar(); /* Get next character from entered phrase */
}
printf("Frequency Table\n");
printf("----------------\n");
printf("Char\tCount\t %% of Total\n");
printf("--------------------------------\n");
printf("All\t%d\t%.2f\n",TL);
for ( TC = 0; TC < 127; TC++)
printf("%c\t%d\t%.2f\n",TC,AA);
system("Pause");
return(0);
}
This is what my final output should look like (but is not working!):
Enter a line of text: This is a test
FREQUENCY TABLE
---------------
Char Count % of Total
---- ----- ----------
ALL 14 100.00%
" " 3 21.43%
"T" 1 7.14%
"a" 1 7.14%
"e" 1 7.14%
"h" 1 7.14%
"i" 2 14.29%
"s" 3 21.43%
"t" 2 14.29%
Press any key to continue . . .
Any help would be greatly appreciated as i am getting very frustrated...
RobBrown