C program to find the frequency of characters in a string: This program counts the frequency of characters in a string, i.e., which character is present how many times in the string. For example, in the string "code" each of the characters 'c,' 'd,' 'e,' and 'o' has occurred one time. Only lower case alphabets are considered, other characters (uppercase and special characters) are ignored. You can modify this program to handle uppercase and special symbols.
الأجوبة
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c = 0, count[26] = {0}, x;
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') {
x = string[c] - 'a';
count[x]++;
}
c++;
}
for (c = 0; c < 26; c++)
printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
return 0;
}
outputs:
Enter a string
a occurs 0 times in the string.
b occurs 0 times in the string.
c occurs 0 times in the string.
d occurs 0 times in the string.
e occurs 0 times in the string.
f occurs 0 times in the string.
g occurs 0 times in the string.
h occurs 0 times in the string.
i occurs 0 times in the string.
j occurs 0 times in the string.
k occurs 0 times in the string.
l occurs 0 times in the string.
m occurs 0 times in the string.
n occurs 0 times in the string.
o occurs 0 times in the string.
p occurs 0 times in the string.
q occurs 0 times in the string.
r occurs 0 times in the string.
s occurs 0 times in the string.
t occurs 0 times in the string.
u occurs 0 times in the string.
v occurs 0 times in the string.
w occurs 0 times in the string.
x occurs 0 times in the string.
y occurs 0 times in the string.
z occurs 0 times in the string.
القوائم الدراسية التي ينتمي لها السؤال