Hacker Rank

Digit Frequency in C HackerRank Solution

Hello Friends in this article i am gone to share Hackerrank C programming Solutions with you. | Digit Frequency in C HackerRank Solution.

Objective

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

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

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

Hackerrank  C programming Solutions

Digit Frequency in C HackerRank Solution

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

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */ 

    char s;
    int i,a[] ={0,0,0,0,0,0,0,0,0,0};
    while(scanf("%c", &s) == 1)
        if(s >= '0' && s <= '9')
            a[s-'0']+=1;
                        
    for(i=0;i<10;i++)
        printf("%d ",a[i]);  
    return 0;
}

What is frequency of digit in c?

Increment the frequency of a digit found above i.e. lastDigit . To increment frequency perform freq[lastDigit]++ . The value of lastDigit will be always between 0-9 inclusive. Hence it can be used as an index to freq array.

What is a frequency of a digit?

Suppose a number is like 12452321, the digit D = 2, then the frequency is 3. To solve this problem, we take the last digit from the number, then check whether this is equal to d or not, if so then increase the counter, then reduce the number by dividing the number by 10.

I hope this article help you , for more C programming Hackerrank Solution follow my website and this link..

Link update Soon: