We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. C
  3. Arrays and Strings
  4. Digit Frequency
  5. Discussions

Digit Frequency

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1110 Discussions, By:

recency

Please Login in order to post a comment

  • nethikrishnakar1
    1 week ago+ 0 comments

    int main() {

    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 1);
    char* pos=s;
    int c,i;
    for(i=0;i<=9;i++){
        char j=i+48;
        for(c=0;*s;s++){
            if((*s)==j)
                c++;
        }
        printf("%d ",c);
        s=pos;
    }
    
    return 0;
    

    }

    0|
    Permalink
  • ergaibi_mhdi
    2 weeks ago+ 0 comments
    #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 */   
        
        int fre0=0, fre1=0, fre2=0, fre3=0, fre4=0, fre5=0, fre6=0, fre7=0, fre8=0, fre9=0;
        char s[10000];
        scanf("%[^\n]", s);
        
        for( int i = 0; i < strlen(s); i++ ){
            switch (s[i]) {
                case '0': fre0++; break;
                case '1': fre1++; break;
                case '2': fre2++; break;
                case '3': fre3++; break;
                case '4': fre4++; break;
                case '5': fre5++; break;
                case '6': fre6++; break;
                case '7': fre7++; break;
                case '8': fre8++; break;
                case '9': fre9++; break;
            }
        }
        printf("%d %d %d %d %d %d %d %d %d %d", fre0, fre1, fre2, fre3, fre4, fre5, fre6, fre7, fre8, fre9);
         
        return 0;
    }
    
    0|
    Permalink
  • dilencevi
    2 weeks ago+ 0 comments

    In C:

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main() {
    
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        /* When accepting characters, maintain an array of integers 0 - 9. An index of an array denotes the digit itself and the value
        denotes the frequency of that index/digit */
        
        int arr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        char ch;
        int idx = 0;
        
        while((ch = getchar()) != '\n' && ch != EOF){
            if(isdigit(ch)){
                idx = ch - '0';  /* Need to subtract '0' (48 in ASCII) to get actual value of a digit */
                arr[idx]++;
            }
        }
        
        for(int i = 0; i < 10; ++i){
            printf("%d ", arr[i]);
        }
        printf("\n");
            
        return 0;
    }
    
    0|
    Permalink
  • srujanachary2004
    4 weeks ago+ 0 comments

    include

    int main() { char str[1000]; int digit_count[10] = {0};

    // Input string
    printf("Enter a string: ");
    scanf("%s", str);
    
    // Iterate through the characters of the input string
    for (int i = 0; i < strlen(str); i++) {
        if (str[i] >= '0' && str[i] <= '9') {
            int digit = str[i] - '0';
            digit_count[digit]++;
        }
    }
    
    // Print the frequency of each digit
    printf("Frequency of each digit from 0 to 9: ");
    for (int i = 0; i < 10; i++) {
        printf("%d ", digit_count[i]);
    }
    
    0|
    Permalink
  • herdeybayor
    1 month ago+ 0 comments
    #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 *str = (char*) malloc(1024);
        int nums[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        
        if(str == NULL) {
            printf("Memory allocation failed\n");
            return 1;
        }
        
        scanf("%s", str);
    
        for(int i; str[i] != '\0'; i++)
        {
            if (str[i] >= '0' && str[i] <= '9')
            {
                int num = str[i] - '0';  // Convert char to int
                nums[num]++;
            }
        }
        
        for (int i=0; i<10; i++)
        {
            printf("%d ", nums[i]);
        }
        
        free(str);
        return 0;
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy