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

1) Use the code tags as mentioned.
2)

TC = getchar();

Has been used 2 times in a loop, the second value is being over written and is lost.
3) And the construct (TC = getchar() != '\n') actually is read by the compiler as (TC = (getchar() != '\n')) .
Hence it gives TC a value of 1 meaning true until '\n', where it gives 0.
So, use either one of the constructs.

while ( (TC = getchar()) != '\n')
{
//...
}

or

TC = getchar();
while ( TC != '\n')
{
//...
TC = getchar();
}

4)When you are printing the results you are passing only two parameters when 3 are required. The third is the percentage calculation. Make sure to cast the value to float before you calculate the percentage. (float)AA[TC]/TL)

I am really having a problem with the for statement.
This is the hint that my teacher gave me and i am not understanding:

"The array element that corresponds with
that character's ASCII value is incremented by one along with a counter
called textLength that tracks the number of entered characters. The
frequency table headers, textLength, and the percentage of all the
characters printed. Then a "for" loop incremented by the 128 elements
in the standard ASCII table uses a condition to check if the value of
the array element is not zero. If true, the text character, the frequency
the percentage (by dividing the element by textLength) is printed."

I have tried
[for (TC = 0; TC > 127; TC++)
printf("%c\t%d\t%.2f\n", TC, AA[TC], ((float) TC / TL) * 100);]

and that does not seem to work...I have tried substituting all the other variables also but it does not work either.

P.S. I tried the code tag but i'm not sure i did it right so i moght need help learning that also.

Code tags explained:
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used, even in responses to your posts
6) Even on the background of the box you actually typed your message in!

Somewhere in those places you should become a CODE TAG expert.

"The array element that corresponds with
that character's ASCII value is incremented by one ...

You have an array of 128 items (call it charList ), each one corresponds to one character in the ASCII chart. The array element (a character from the input array) is used as an index into charList and that entry is incremented. That character is now counted.

... along with a counter called textLength that tracks the number of entered characters.

textLength is incremented for each and every character and counts the total number of characters entered.

The frequency table [charList] , headers [?] , textLength, and the percentage of all the characters printed.

I think you missed something here.

Then a "for" loop incremented by the 128 elements in the standard ASCII table uses a condition to check if the value of the array element is not zero. If true, the text character, the frequency the percentage (by dividing the element by textLength) is printed."

A for loop goes from 0 to 127 and tests each value in charList and if a value is > 0, the character was in the input. Output all the information for that character. If < 0 output nothing. Remember, the index of the loop is also the character itself, so you can simply output the loop counter as a %c.

I have tried
[for (TC = 0; TC > 127; TC++)
printf("%c\t%d\t%.2f\n", TC, AA[TC], ((float) TC / TL) * 100);]

and that does not seem to work...I have tried substituting all the other variables also but it does not work either.

There is no test to see if the character was actually in the input.
Blindly substituting variables is a very bad way to try to guess the answer.

Here is your modifed code...changes are marked ac CC in comments

#include <stdio.h>
#include <stdlib.h>

int main()

{
int i;
	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: ");
	TC = getchar();					//CC-1
	while (TC != '\n')				//CC-1
	{
    TL++; /* Counts each character */
    AA[(int)TC]++; /* Increments array element that corresponds with the character's 
              ASCII value */    //CC-3 it's better to type cast though it will work
    TC = getchar();
     }

	printf("Frequency Table\n");
	printf("----------------\n");
	printf("Char\tCount\t %% of Total\n");
	printf("--------------------------------\n");
	printf("All\t%d\n",TL);
	for ( i = 0; i < 127; i++)
	if(AA[i]>0)							//modified portion
	printf("%c\t%d\t%.2f\n",i,AA[i],(float)AA[i]/TL*100);
    
	
	return 0;
}

Thank you, is there a way to have the output sorted so the most frequent characters are appearing first (highest count first) and when multiple characters have the same count then sort the output by ASCII code in ascending order (normal alphabetical order).

You can try to run through the whole AA array using a loop and then copy the index (character) of each element that has value 0 (one referring characters that dont appear in input text) to the end of a new char array. then you copy all the elements that appeared only once, then all that appeared 2 times and so on...

when to stop?
while doing this you count total number of chars put into new array, and when it reaches the number of total characters inputted you dont have to worry are there any characters left uncopied

remember that you will need double loop. one to loop through different possible frequencies (0, 1, 2, 3....) and other to search for all characters that have that frequency value.

i will let you think about how to make characters appear in alphabetical order. (dont think about that at the moment, after you do this print the array and you will notice everything that you need to notice)

cheers :D

i have given you the code..if u have some other modifications then you try it by yourself so that u can learn something..!!!and at least post the code u tryed so that we can help you to find the error..!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.