Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to beat a Hacker Rank challenge where I am required to come up with C code that counts the frequency of the digits 0 to 9 in a string and print the frequencies in a line. If a digit is not found then the program should output zero for that number. I have managed to come up with this code that passes 4 out of the ten test cases and prints the frequency of digits in a string containing a mixture of digits and other symbols. However the other 6 test cases fail and the compiler shows the message Segmentation Fault, I have researched this error and learnt that it occurs when the program tries to access memory that does not belong to it, or maybe the string for the other test cases is too large to be passed as a parameter to my function. Help my fix my code so it passes the other test cases.
My code is shown below

What I have tried:

<pre lang="C++">

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

void countDigits(const char* string) {
   if (string == NULL) {
        printf("Input string is NULL.\n");
        return;
    }

    int digit_frequency[10] = {0}; // Array to store the frequency of each digit
    
    // Iterate over each character in the string
    for (int i = 0; string[i] != '\0'; i++) {
        if (isdigit(string[i])) {
            int digit = string[i] - '0'; // Convert the character to integer
            digit_frequency[digit]++; // Increment the frequency of the digit
        }
    }
    
    // Print the frequency of each digit or 0 if not found
    for (int i = 0; i < 10; i++) {
        printf("%d ", digit_frequency[i]);
    }
}

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    //read the input from the string
    char name[50];
    scanf("%s", &name);
    //scan the string for digits
    countDigits(name);
    
    return 0;
}
Posted
Updated 30-May-23 5:29am

Quote:
char name[50];

The size of the array could be 'not enough'.
 
Share this answer
 
Comments
Tim the Gamer 30-May-23 8:27am    
In the main where I am capturing it from the user? Should I change from 50 to some higher number?
CPallini 30-May-23 8:32am    
Yes, you might change that.
You may also consider using fgets instead of scaf. See, for instance:
https://www.geeksforgeeks.org/why-to-use-fgets-over-scanf-in-c/
Tim the Gamer 30-May-23 8:38am    
it worked, Thanks. I didn't see a constraint in the challenge for the input which needed a maximum limit of 1000 buffer length
CPallini 30-May-23 8:54am    
You are welcome.
Mr. Pallini gave you some very important advice I thought I would elaborate on. He recommended using fgets and I agree completely. It in conjunction with a constant is the best option. Here's an example :
C++
const int BufferSize = 1000;
char buffer[ BufferSize + 1 ] = { 0 };   // +1 allows room for a null
fgets( buffer, BufferSize, stdin );
The important things there are the +1 in the declaration of buffer to allow room for the null character, initializing the buffer to 0 to force the trailing null to be there, and then using fgets without the +1 so the null character remains.

This same tactic with the +1 also works with strncpy, strncat, snprintf, and other functions that take a buffer size parameter. You need to remember to initialize the buffer to zeros to ensure the trailing null is there though because in debug mode buffers are not always initialized to zero.
 
Share this answer
 
Comments
Tim the Gamer 30-May-23 11:51am    
Thanks for your clarification. I will try the code you provided and see if it works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900