Dear C programmers, I need your help in this assignment, It must be in C not at
ID: 640743 • Letter: D
Question
Dear C programmers, I need your help in this assignment,
It must be in C not at C++
Create a program called arrays.c that does the following:
Prompt the user for a string (<= 100 characters).
Display the frequency table showing the total number of characters entered by the user, the count for each character entered by the user, and the percentage of the total count that is attributed to each character.
The absolute frequency of a character is the number of times the character appears. For example, in the string "aaab" the absolute frequency of 'a' is 3, and the absolute frequency of 'b' is 1.
The relative frequency of a character is the absolute frequency divided by the total number of characters. For example, in the string "aaab" the relative frequency of 'a' is 3/4 = 0.75, and the relative frequency of 'b' is 1/4 = 0.25. Relative frequency * 100 will give you the result in percentage
Here is my source code file. The program must count whitespace too. My program counts whitespaces but it shows a character (" 1 ) that the user do not enter this character and involve this character in the general average. Sometimes is wrong in mi code. Can you help to find it please? Please compile the source below and write a string. If you enter a string of 5 words it shows 6 for example.
below is an example how the program must be
Program: Array
Author: Doug Jones
Enter string 1: THIS IS a test of string 0123456789 +~_!)@()#($*%&^ stuff
FREQUENCY TABLE
---------------------------
Char Count % of Total
---- ----- ----------
ALL 58 100.00
" " 9 15.52
"!" 1 1.72
"#" 1 1.72
"$" 1 1.72
"%" 1 1.72
"&" 1 1.72
"(" 2 3.45
")" 2 3.45
"*" 1 1.72
"+" 1 1.72
"0" 1 1.72
"1" 1 1.72
"2" 1 1.72
"3" 1 1.72
"4" 1 1.72
"5" 1 1.72
"6" 1 1.72
"7" 1 1.72
"8" 1 1.72
"9" 1 1.72
"@" 1 1.72
"H" 1 1.72
"I" 2 3.45
"S" 2 3.45
"T" 1 1.72
"^" 1 1.72
"_" 1 1.72
"a" 1 1.72
"e" 1 1.72
"f" 3 5.17
"g" 1 1.72
"i" 1 1.72
"n" 1 1.72
"o" 1 1.72
"r" 1 1.72
"s" 3 5.17
"t" 4 6.90
"u" 1 1.72
"~" 1 1.72
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[101];
int count[128] = {0};
int length = 0;
int i;
printf("Enter a string: ");
fgets(str,100,stdin);
length = strlen(str);
for (i = 0; i < length; i++)
{
count[(int)str[i]]++;
}
printf("FREQUENCY TABLE ");
printf("------------------------- ");
printf("Char Count %% of Total ");
printf("------------------------- ");
printf("ALL %d 100.00% ", length);
for (i = 0; i < 128; i++)
{
if (count[i])
{
printf(""%c" %d %0.2f% ", i, count[i], (float)((count[i]*100)/length));
}
}
getch();
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
int main(void){
char str[101];
int count[128] = {0};
int length = 0;
int i;
printf("Enter a string: ");
fgets(str, 100, stdin);
length = strlen(str);
length--; // this is the only modification
for (i = 0; i < length; i++){
count[(int)str[i]]++;
}
printf("FREQUENCY TABLE ");
printf("------------------------- ");
printf("Char Count %% of Total ");
printf("------------------------- ");
printf("ALL %d 100.00% ", length);
for (i = 0; i < 128; i++){
if (count[i]){
printf(""%c" %d %0.2f% ", i, count[i], (float)((count[i]*100)/length));
}
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.