Digit Frequency problem solution | C | HackerRank

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 .




#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]);
    printf("\n");
    free(s);
    return 0;
}

Comments

Popular posts from this blog

Insertion Sort - Part 2 | C | HACKERRANK

Array Reversal problem solution | C | HackerRank

Insertion Sort - Part 1 | C | HackerRank |