Digit Frequency in C – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Digit Frequency in C-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

C is one of the most widely used Programming Languages. it is basically used to build Operating System. C was developed by Dennis Ritchie in 1972. Below are some examples of C Programming which might you understanding the basics of C Programming.


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 :

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


Sample Output 0

0 2 1 0 1 1 1 1 0 0 

Explanation 0

In the given string:

  • 1 occurs two times. 
  • 2,4,5,6 and 7 occur one time each.
  • The remaining digits 0,3,8 and 9 don’t occur at all.

Sample Input 1


Sample Output 1

0 2 1 0 1 0 0 0 2 0 

Sample Input 2


Sample Output 2

1 1 1 2 0 1 2 0 5 0 
Digit Frequency in C – Hacker Rank Solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%s", s);
    s = realloc(s, strlen(s) + 1);
    int len = strlen(s), i;
    int arr[10];
    for(i = 0; i < 10; i++)
        arr[i] = 0;
    for(i = 0; i < len; i++) {
        if(s[i] >= '0' && s[i] <= '9') {
            arr[(int)(s[i] - '0')]++;
    for(i = 0; i < 10; i++)
        printf("%d ", arr[i]);
    return 0;

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock