Monday, May 2, 2022

Digit Frequency in C

 Given a string, , consisting of alphabets and digits, find the frequency of each digit in the given string.

Input Format

The first line contains a string,  which is the given number.

Constraints


All the elements of num are made of english alphabets and digits.

Output Format

Print ten space-separated integers in a single line denoting the frequency of each digit from  to .

Sample Input 0

a11472o5t6

Sample Output 0

0 2 1 0 1 1 1 1 0 0 

Explanation 0

In the given string:

  •  occurs two times.
  •  and  occur one time each.
  • The remaining digits  and  don't occur at all.

Sample Input 1

lw4n88j12n1

Sample Output 1

0 2 1 0 1 0 0 0 2 0 

Sample Input 2

1v88886l256338ar0ekk

Sample Output 2

1 1 1 2 0 1 2 0 5 0 
SOLUTION:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>

int main() {

    char str[1000],s;
    int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,m=0,j=0;
    
    scanf("%s",str);
    
    for(int i=0;str[i]!='\0';i++)
    {
        s=str[i];
          
            switch(s)
            {
            case '0': a++;
                          break;
            case '1': b++;
                          break;
            case '2': c++;
                          break;
            case '3': d++;
                          break;
            case '4': e++;
                          break;
            case '5': f++;
                          break;
            case '6': g++;
                          break;
            case '7': h++;
                          break;
            case '8': m++;
                          break;
            case '9': j++;
                          break;
            defaultbreak;
               
            }
        
        
    }
    printf("%d %d %d %d %d %d %d %d %d %d",a,b,c,d,e,f,g,h,m,j);
    return 0;
    
}

No comments:

Post a Comment

Featured Post

14. Longest Common Prefix

Popular Posts