Write a program that will prompt the user for a string. Read the string using ge
ID: 3938573 • Letter: W
Question
Write a program that will prompt the user for a string. Read the string using gets(). Convert the string to all capitals using your my_strupper function from the earlier problem. Next, write a loop to cycle thru each character of the string, and count the occurance of each letter. Store these values in int freqs[26];//hmmm, what does this do?? freqs[str[i] - 'A']++;//hmmm, and this?? printf("%c %d ", i + 'A', freqs[i]); Sample Output: Please enter a string: Foobar A 1 B 1 F 1 O 2 R 1 Press any key to continue...Explanation / Answer
Answer:-
#include <stdio.h>
#include <string.h>
void find_frequency(char [], int []);
int main()
{
char string[100];
int c, count[26] = {0};
printf("Input a string ");
gets(string);
find_frequency(string, count);
printf("Character Count ");
for (c = 0 ; c < 26 ; c++)
printf("%c %d ", c + 'a', count[c]);
return 0;
}
void find_frequency(char s[], int count[]) {
int c = 0;
while (s[c] != '') {
if (s[c] >= 'a' && s[c] <= 'z' )
count[s[c]-'a']++;
c++;
}
}
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.