C Program to calculate the Frequency of characters in a string with pointer code example
Example 1: Write a c program to count the different types of characters in given string.
#include<stdio.h>
#include<string.h>
void main() {
char arr[]="askjdfbajksdfkasdfhjkasdfh";
char x;
int l=strlen(arr);
int i=0,j;
int c=0,var=0;
for(j=97;j<=122;j++) {
for(i=0;i<l-1;i++) {
if(arr[i]==j) {
c++;
}
}
if(c>0) {
var++;
printf("No. of Occurence of %c ::%d\n",j,c);
}
c=0;
}
printf("No. of type of characters:%d\n",var);
}
Example 2: c program to the count the number of times each character appears
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i) {
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}