This program is used to find the character count in a given string.The output is character list followed by its count.I got this question in an interview.I hope it'll be helpful to you.
Program:
#include<stdio.h>char str[20],str1[20],size=1;
void main()
{
int count[20],c,i;
printf("Enter string :\n");
gets(str);
str1[0]=str[0];
count[0]=1;
for(i=1;str[i]!='\0';i++)
{
c=found(str[i]);
if(c==-1)
{
str1[size]=str[i];
count[size]=1;
size++;
}
else
{
count[c]++;
}
}
for(i=0;i<size;i++)
{
printf("%c----->%d\n",str1[i],count[i]);
}
}
int found(char c) //To find whether the character is already found or not
{
int i;
for(i=0;i<size;i++)
{
if(c==str1[i])
{
return i;
}
}
return -1;
}
0 comments:
Post a Comment