Feb 7, 2022

HackerRank-Printing Frequency of digits in Number (C language)

Printing the frequency of digits (0 to 9). 

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

int main() {
    char str[1000];
    scanf("%s" , str);
    int arr[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    int i = 0;
    while (str[i] != '\0')
    {
        if (str[i] == '0' || str[i] == '1' || str[i] == '2' || str[i] == '3' ||
            str[i] == '4' || str[i] == '5' ||
            str[i] == '6' || str[i] == '7' || str[i] == '8' || str[i] == '9')
        {
            arr[str[i] - '0']++;
        }

        i++;
    }
    for (int i = 0; i < 10; i++)
    {
        printf("%d " , arr[i]);
    }



    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...