Algorithm


Problem Name: Digit Frequency

Problem Link:https://www.hackerrank.com/challenges/frequency-of-digits-1/problem?isFullScreen=true

In this HackerRank Functions in C programming problem solution,

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

Input Format

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

Constraints

1 <= len(num) <= 1000

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 0 to 9.

Sample Input 0

a11472o5t6

Sample Output 0

0 2 1 0 1 1 1 1 0 0 

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


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

int main()
{
    char s[1001];
    scanf("%[^\n]%*c", s);
    int arr[10] = {0};
    for(int i = 0; i  <  strlen(s); i++){
        if(s[i] >= '0' && s[i] <= '9')
            arr[s[i] - '0']++;
    }
    for(int i = 0; i  <  10; i++)
        printf("%d ", arr[i]);
    printf("\n">;
    
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
a11472o5t6

Output

x
+
cmd
0 2 1 0 1 1 1 1 0 0
Advertisements

Demonstration


Previous
[Solved] Printing Tokens in C solution in Hackerrank - Hacerrank solution C
Next
[Solved] Dynamic Array in C in C solution in Hackerrank - Hacerrank solution C