4) (20 marks) Write a C program named a5q4.c which reads standard input and repo
ID: 3604552 • Letter: 4
Question
4) (20 marks) Write a C program named a5q4.c which reads standard input and reports at the end the counts and frequencies of all letters and the space character. The program must normalize the letters, i.e., must count as the same uppercase and lowercase letters. The count is the number of times a letter occured in text, while frequency is a fraction of time the letter occured divided by the total number of letters. (Be careful to avoid dividing by zero!)
Any characters between letters, i.e, between words, are counted as a simple space character, which is printed as an underscore (`_') character. You should also assume that there is a space character at the very beginning and the very end of the text.
If the input is empty or if it does not contain any letters, the output should be the same as only one space was processed.
The program output must be as in the sample output shown below. You should align the numbers as shown in the table, and the frequency is printed in percentages rounded to exactly four decimals. (The percentages mean that the actual fraction is multiplied with 100.)
Make sure that your program produces format as given in the sample output.
Sample Input:
i.e., must count as the same uppercase and lowercase letters. The count is the number of times a letter occured in text, while frequency is a fraction of time the letter occured divided by the total number of letters. (Be careful to avoid dividing by zero!)
Any characters between letters, i.e, between words, are counted as a simple space character, which is printed as an underscore (`erb,_,') character.
Sample Output
Letter occurences freq.
-------------------------
_ 71 18.25 %
a 24 6.17 %
b 8 2.06 %
c 21 5.40 %
d 13 3.34 %
e 53 13.62 %
f 6 1.54 %
g 1 0.26 %
h 11 2.83 %
i 19 4.88 %
j 0 0.00 %
k 0 0.00 %
l 10 2.57 %
m 7 1.80 %
n 16 4.11 %
o 16 4.11 %
p 5 1.29 %
q 1 0.26 %
r 27 6.94 %
s 19 4.88 %
t 33 8.48 %
u 12 3.08 %
v 4 1.03 %
w 6 1.54 %
x 1 0.26 %
y 4 1.03 %
z 1 0.26 %
-------------------------
Explanation / Answer
Here is the code for you:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void my_strupper(char *string)
{
for(int i = 0; *(string+i) != ''; i++)
if(*(string+i) >= 97 && *(string+i) < 124)
*(string+i) = *(string+i) - 'a' + 'A';
}
int main()
{
char line[256];
//Store these values in int freqs[26]
int freqs[27];
for(int i = 0; i < 27; i++)
freqs[i] = 0;
//Prompt the user for a string
printf("Please enter a string: ");
//Read the string using gets().
gets(line);
//Convert the string to all capitals using your my_strupper function from the earlier problem.
my_strupper(line);
//Next, write a loop to cycle thru each character of the string, and count the occurance of each letter.
for(int i = 0; i < strlen(line); i++)
if(isspace(line[i]))
freqs[0]++;
else if(isalpha(line[i]))
freqs[line[i] - 64]++;
//Lastly, use freqs and a loop to output a table of these letters and their frequencies, ommiting those with a frequency of zero.
printf("_ %i %.2f%% ", freqs[0], freqs[0]/(float)strlen(line)*100);
for(int i = 1; i < 27; i++)
printf("%c %i %.2f%% ", 'a'+i-1, freqs[i], freqs[i]/(float)strlen(line)*100);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.