4. [8] Write a C function with prototype void letter_freq(const char wordll, int
ID: 3594127 • Letter: 4
Question
4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This function computes the number of appearances of each letter in the string word and stores them irn array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90 for the uppercase letters. You must account for uppercase and lowercase letter variants, which should be counted together. The counts have to be stored in array freq in alphabetical order of letters, which corresponds to the increasing order of their ASCII values. Specifically, freq[0] should store the count of" and ‘a", freq [1] should store the count of ‘B' and ‘b, and so on. This function has also to print the counts indicating each letter and its count on a separate line, as follows: The count of A'and a'is. The count of 'B' and 'b' is.. Write a program to test the function. Hint: If variable x of type char represents a lower case letter, then the corresponding index in the array equals the integer value of x-'a'. If x is an upper case letter, then the index in the array equals x-'A'.Explanation / Answer
PLEASE REFER BELOW CODE
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
void letter_freq(const char word[], int freq[])
{
int i;
for(i = 0; word[i] != ''; i++) //travelling each alphabet of word
{
if(isupper(word[i])) //if alphabet is uppercase then we are subtracting 65
freq[word[i] - 65]++;
else
freq[word[i] - 97]++; //if it's lowercase we are subtracting 97
}
}
int main()
{
char test_string[20] = "Chegg"; //test string
int freq[26]={0}; //array to count frequency and initialised to 0
int i;
letter_freq(test_string, freq); //calling function
for(i = 0; i < 26; i++) //printing frequency
{
printf("The count of '%c' and '%c' is %d ",('A' + i), ('a' + i), freq[i]);
}
return 0;
}
PLEASE REFER BELOW OUTPUT
The count of 'A' and 'a' is 0
The count of 'B' and 'b' is 0
The count of 'C' and 'c' is 1
The count of 'D' and 'd' is 0
The count of 'E' and 'e' is 1
The count of 'F' and 'f' is 0
The count of 'G' and 'g' is 2
The count of 'H' and 'h' is 1
The count of 'I' and 'i' is 0
The count of 'J' and 'j' is 0
The count of 'K' and 'k' is 0
The count of 'L' and 'l' is 0
The count of 'M' and 'm' is 0
The count of 'N' and 'n' is 0
The count of 'O' and 'o' is 0
The count of 'P' and 'p' is 0
The count of 'Q' and 'q' is 0
The count of 'R' and 'r' is 0
The count of 'S' and 's' is 0
The count of 'T' and 't' is 0
The count of 'U' and 'u' is 0
The count of 'V' and 'v' is 0
The count of 'W' and 'w' is 0
The count of 'X' and 'x' is 0
The count of 'Y' and 'y' is 0
The count of 'Z' and 'z' is 0
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.