Frequncy Of Each Character

C program to read a string and output the frequency of each character in that string. Source Code: #include <stdio.h> #include <conio.h> #include<string.h> int main() { char string[100]; int c = 0, count[26] = {0}; clrscr(); printf("Enter a string\n"); 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 occurs %d times in the entered string.\n",c+'a',count[c]); } getch(); return 0; } output: Enter a string He is a very good boy a occurs 1 times in the entered string. b occurs 1 times in the entered string. d occurs 1 times in the entered string. e occurs 2 times in the entered string. g occurs 1 times in the entered string. i occurs 1 times in the entered string. o occurs 3 times in the entered string. r occurs 1 times in the entered string. s occurs 1 times in the entered string. v occurs 1 times in the entered string. y occurs 2 times in the entered string.

Share:

0 comments