4) (20 marks) Write a C program named a5q4.c which reads standard input and repo
ID: 3605633 • 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 frequencv is a fraction of time the letter occured divided by the total number of letters. (Be careful to avoid dividing y zero! Any characters between letters, i.e, between words, are counted as a simple space char acter, which is printed as an underscore ("_') character. You should also assume that ther 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 alig 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 Sample input and output are given below, provided on the web site, and in the director "prof2132/public on bluenose 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 letter:s 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:Explanation / Answer
#include<stdio.h>
#include<string.h>
int main(){
char string[100], ch;
int c = 0, count[26] = {0};
int total=0;
float per;
printf("Enter a string ");
gets(string);
while ( string[c] != '' ){
/* Considering characters from 'a' to 'z' only */
if ( string[c] >= 'a' && string[c] <= 'z' )
count[string[c]-'a']++;
c++;
}
for ( c = 0 ; c < 26 ; c++ ){
total += count[c];
}
printf("Letter occurances frequency" );
for ( c = 0 ; c < 26 ; c++ ){
if( count[c] != 0 )
printf("%c %d %.2f % ",c+'a',count[c], (count[c]/total)*100.0);
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.