Introduction To Programming

Why programs ? Programming is nothing but making the computers think the same way a human thinks. Programs are made for obtaining a specific solution which can even be solved by humans which when solved by us may consume time..

Tuesday, February 23, 2010

Frequency of characters

Finding frequency characters in a string. Eg: "shark srini" in this string
s:2
h:1
a:1
r:2
k:1
i:2
n:1


#include< stdio.h >
#include< string.h >
int main( )
{
char a[]="shark srini",c[26];
int k,i,j,n,l,s;
k=0;
n=strlen(a);
c[k]=a[k];
k++;
for(i=0;i < n-1;i++)
{
s=1;
for(j=0;j < k;j++)
{
if(c[j]==a[i])
{
s=0;
}
}
if(s!=0)
{
c[k]=a[i];
k++;
}
}
for(j=0;j < k;j++)
{
l=0;
for(i=0;i < n;i++)
{
if(c[j]==a[i])
{
l++;
}
}
printf("%c :\t %d\n",c[j],l);

}
return 0;
}



Output:


s:2
h:1
a:1
r:2
k:1
i:2
n:1