Saturday 26 October 2013

Frequency of the character in a String

frequency of a character means that how many times the character occurs in a string.
Ex:- Ctutorials
frequency of C = 1
frequency of t =2
frequency of o , a , l , s , u =1
frequency of t is 2 because only t comes 2 times


#include<stdio.h>
void main()
{
 char a[20],character; 
int i,freq=0; 
printf("enter the string:- ");
 gets(a); 
printf("enter the character:- ");
 scanf("%c",&character); 
for(i=0;a[i]!='\0';++i) 
{
 if(character==a[i])
 ++freq; 
}
 printf("Frequency of %c = %d", character, freq); 
}

No comments :

Post a Comment