Program to find character frequency in string: To make a count or number of times each character appeared in a string.
We make a count by setting the counter to each character in a string and increment counter as the character used in the string.

Algorithm to find character frequency

  1. Declare variables.
  2. Take a sentence user input
  3. Traverse sentence till end.
    Compare the character of sentences with small and capital letters.
    If character == alphabet
    Counter[alphabet] ++
  4. Print value at each alphabet position from start to end. Counter[]

Program to find character frequency in string

#include<stdio.h>

#include<conio.h>

void main()

{

int i,k=0,count[26]={0},x;

char str1[100];

printf("Enter a sentence\n");

gets(str1);

while(str1[k]!='
#include<stdio.h>

#include<conio.h>

void main()

{

int i,k=0,count[26]={0},x;

char str1[100];

printf("Enter a sentence\n");

gets(str1);

while(str1[k]!='\0')

{

if(str1[k]>='a'&&str1[k]<='z')

{

x=str1[k]-'a';

count[x]++;

}

if(str1[k]>='A'&&str1[k]<='Z')

{

x=str1[k]-'A';

count[x]++;

}

k++;

}

for(i=0;i<26;i++)

{

if(count[i]!=0)

{

printf("%c occured %d times\n",i+'a',count[i]);
}
}
}
') { if(str1[k]>='a'&&str1[k]<='z') { x=str1[k]-'a'; count[x]++; } if(str1[k]>='A'&&str1[k]<='Z') { x=str1[k]-'A'; count[x]++; } k++; } for(i=0;i<26;i++) { if(count[i]!=0) { printf("%c occured %d times\n",i+'a',count[i]); } } }

Output:-

Program to find character frequency in string

Write A Comment