Write a program that will read a line of text and will displ
Write a program that will read a line of text, and will display all letters in the text along with the number of times the letter appears in the text.
Your program should begin by asking the user to enter a line of text. Once the text has been entered, then you should proceed to go through the text, counting the number of times you see each letter. After you have finished counting, display each letter found, along with the number of times the letter occurs.
The following is an example of what your might see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user\'s inputted values are shown below in italics:
Enter a line of text:
How now, brown crow?
Letter frequencies:
b - 1
c - 1
h - 1
n - 2
o - 4
r - 2
w - 4
Solution
The below is the C program that counts the letters in given string.
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c = 0, count[26] = {0};
printf(\"Enter a string\ \");
gets(string);
while (string[c] != \'\\0\')
{
/** Considering characters from \'a\' to \'z\' only
and ignoring others */
if (string[c] >= \'a\' && string[c] <= \'z\')
count[string[c]-\'a\']++;
c++;
}
for (c = 0; c < 26; c++)
{
/** Printing only those characters
whose count is at least 1 */
if (count[c] != 0)
printf(\"%c - %d.\ \",c+\'a\',count[c]);
}
return 0;
}
