Friday, October 14, 2011

Write a C program to find character frequency in a given string? Example: given Input is: I AM AN INDIAN


#include
main()
{
char s[100], ch;
int count= 0, n[26]={0},temp,i;
printf("Enter a string\t:");
gets(s);
printf("Character\t\tFreequency");
printf("\n---------\t\t----------\n");
for (i=0;i<26;i++) { temp=s[i]; if (temp>=65 && temp<=90)
{
temp+=32;
s[i]=temp;
}
else
s[i]=temp;
}

while(s[count]!='\0')
{
n[s[count]-'a']++;
count++;
}
for (count=0;count<26;count++ )
{
if( n[count] != 0 )
printf(" %c\t\t\t %d\n",count+'a',n[count]);
}
}

No comments:

Post a Comment