rumon
rumon

Reputation: 1

hackerrank problem: Given a string, , consisting of alphabets and digits, find the frequency of each digit in the given string

link:https://www.hackerrank.com/challenges/frequency-of-digits-1/problem?isFullScreen=true&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen

where the wrong here? my codeblocks gives right output but in hackerrank it wrong answer.

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

int main() {
    char s[1000];
    char a[10]={0,0,0,0,0,0,0,0,0,0};
    int i;
    scanf("%[^\n]", s);
    int l = strlen(s);
    
    for(i=0; i<l; i++){
        int m = s[i]-48;
        a[m]=a[m]+1;
    }
    
    for(i=0; i<9; i++){
        printf("%d ", a[i]);
    }
     printf("%d", a[9]);
     return 0;
}```

Upvotes: 0

Views: 4979

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

For starters these headers

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

are redundant. The program can be written without using any declaration from the headers.

Instead you need to include the header <stdio.h>

#include <stdio.h>

The array a should have the type size_t. Even for the character array having 1000 elements an object of the type char can be unable to store such big values.

And the initialization of the array a can be simpler

size_t a[10] = { 0 };

Also as it follows from the assignment the character array s should be able to store a string with the length equal to 1000. It means that the character array itself shall have at least 1001 character to be able to store also the terminating zero character '\0' of a string with 1000 characters.

These statements

    int m = s[i]-48;
    a[m]=a[m]+1;

can result in accessing memory beyond the array a that invokes undefined behavior because there is no check whether the current character contains a digit.

The program can look the following way.

#include <stdio.h>

int main( void ) 
{
    char s[1001];
    size_t digits[10] = { 0 };

    if ( scanf( "%1000[^\n]", s ) == 1 )
    {
        for ( size_t i = 0; s[i] != '\0'; i++ )
        {
            if ( '0' <= s[i] && s[i] <= '9' )
            {
                ++digits[s[i] - '0'];
            } 
        } 
    
        for ( size_t i = 0; i < 10; i++ )
        {
            printf( "%c: %zu\n", ( char )( i + '0' ), digits[i] );
        }
    }

    return 0;
}

For example if to enter string "122333444455555666666777777788888888999999999" then the output will look like

0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
7: 7
8: 8
9: 9

If you need to output frequencies in one line then instead of this statement

printf( "%c: %zu\n", ( char )( i + '0' ), digits[i] );

just write

printf( "%zu ", digits[i] );

In this case for the above string the output will be

0 1 2 3 4 5 6 7 8 9 

Upvotes: 0

Mahadev K
Mahadev K

Reputation: 350

If you put the condition m>=0 and m<10 then a[m]++. The code will work fine. Working code. Also updated size to 1001.

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

int main() {
    char s[1001] ;
    char a[10]={0,0,0,0,0,0,0,0,0,0};
    int i;
    scanf("%[^\n]", s);
    int l = strlen(s);
    
    for(i=0; i<l; i++){
        int m = s[i]-48;
        if(m>=0 && m<10)
        a[m]++;
    }
    
    for(i=0; i<=9; i++){
        printf("%d ", a[i]);
    }
     return 0;
}

Upvotes: 1

Related Questions