My code for finding the frequency of digits in a character array in C is not giving the desired output.
int main() {
int n;
scanf("%d",&n);
char str[n];
int f[10],i,j,ns;
gets(str);
for(i=0;i<10;i++)
{
f[i]=0;
for(j=0;j<strlen(str);j++)
{
ns=str[j]-'0';
if(i==ns)
f[i]+=1;
}
}
for(i=0;i<10;i++)
printf("%d ",f[i]);
return 0;
}
If I enter the string
1wjhue73b38409598
I am getting the output of frequency of (0-9):
1 0 0 2 1 1 0 1 2 2
Instead of frequency 1 for '1'. Where am I going wrong?