0

I have a string of alphabets and digits , in which i have to find the frequency of each digit from 0 to 9.

i have taken nested for loops , first for digits and second for string. then compare both with the use of if.. else..

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int i,j,count=0;
char s[1000];
scanf("%s",s);
for(i=0;i<=9;i++)
{
    for(j=0;j<strlen(s);j++)
    {
        if(i==s[j])
        {
            count=count+1;
        }    
    }
    printf("%d ",count);
    count=0;
    }  
return 0;
}

if input is 'abdf2343' then output must be 0 0 1 2 1 0 0 0 0 0. but my output is 0 0 0 0 0 0 0 0 0 0

Bhargav Desai
  • 941
  • 1
  • 5
  • 17

1 Answers1

0

try this code please:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() { 
    char s[1000];
    scanf("%s",&s);
    char str[10]={'0','1','2','3','4','5','6','7','8','9'};   
    int count=0;
    for(int i=0;i<=9;i++)
    {
        for(int j=0;j<strlen(s);j++)
        {
            if(str[i]==s[j])
            {
                count=count+1;
            }
        }
        printf("%d ",count);
        count=0;
    }
    return 0;
}