0

I was solving a question this is my code its working perfect on my local Work space with the below input and gives the expected output but in hacker rank it is a time out for compilation .

The problem statement is In the given string:

occurs two times. and occur one time each.

The remaining digits and don't occur at all.

Sample Input 1: lw4n88j12n1

Sample Output 1 : 0 2 1 0 1 0 0 0 2 0

Facing Issue with below input

Input:

9139f793308o0lo66h6vc13lgc697h0f6c32lu84445972k0o0l033od17c083yn5051d6j319hyo8j939n28d913015ns6zx5653x01211x12ch2526o65sg7xw6302141q9203s22l336319ll9yx4b597mr318a7943906750j4u152067nq83ne9f24thu96yd05173l47c803roxci45615f0w53i1sz913jj6za733l73tw6r66mq6p44sfhjr26h8e801z8zlcx2l1e65r2879xj3w3acv216196uq158o663y7oz2i5378v0v5w17762451t424352m23026r9o202i9785382o159e4gu1c8561157z5f1vqs5755465b8u728u956434mv944885li456628a994u7j5278m269n1pk8e46940q834h06il6h447888tr7ig72z10fe09k5g98h9bgt6z40v42s16pt6k3l3v45i83i01b9448g554741w766f2q7v31i085488h060e710p53076c6nm98pi946g8j2n6j8x29qa1ad48172y0u4818121p686bud89741201p54087u56g8scerv9pvhuo09re477zfb224i2c1325bj58jx4bk7b009f6446j5i95474p266i503r670n631x6940gwl71ejbx47imx576129248901765rnpu6l80084t0j1839f5y3409w2n403fu6ogw1170jmb6o5l520vg0703e0

Expected Output: 53 54 47 48 54 52 63 46 49 46

Thanks for the help in advance.

int main() {

    int i;
    int value;
    char x;
    int arr[10] = {0};
    char * ptr =(char *)malloc(sizeof(char *));
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    scanf("%s",ptr);

     while(*ptr != '\0')
    {
        if(*ptr >= 65 && *ptr <=90 || *ptr >=97 && *ptr <=122){
            *ptr++;
        }
        else{
            x = *ptr;
            value = atoi(&x);
            switch (value)
               {
                case 0:
                    arr[0]++;
                    *ptr++;
                    break;
                case 1:
                    arr[1]++;
                    *ptr++;
                    break;
                case 2:
                    arr[2]++;
                    *ptr++;
                    break;
                case 3:
                    arr[3]++;
                    *ptr++;
                    break;
                case 4:
                    arr[4]++;
                    *ptr++;
                    break; 
                case 5:
                    arr[5]++;
                    *ptr++;
                    break;
                case 6:
                    arr[6]++;
                    *ptr++;
                    break;
                case 7:
                    arr[7]++;
                    *ptr++;
                    break;
                case 8:
                    arr[8]++;
                    *ptr++;
                    break;
                case 9:
                  arr[9]++;
                  *ptr++;
                  break;
               }
        }
    }

    for(i=0;i<=9;i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • 3
    `sizeof(char *)` is the size of the *pointer*, which is usually 4 or 8 bytes (depending on if you're on a 32 or 64 bit system). Reading more than that in your `scanf` call (remembering that you need space for the terminator at the end as well) will cause *undefined behavior*. – Some programmer dude May 13 '19 at 12:20
  • 2
    Also, in C you [should not cast the return or `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/). – Some programmer dude May 13 '19 at 12:21
  • 4
    BTW your `switch` is rather pointless, you can simplify this to 1 or 2 lines. – Jabberwocky May 13 '19 at 12:22
  • 3
    ... and please elaborate what this is supposed to mean: _"The problem statement is In the given string: occurs two times. and occur one time each. The remaining digits and don't occur at all."._ I don't understand this at all. And your question title is also rather meaningless... – Jabberwocky May 13 '19 at 12:24
  • 4
    Lastly another couple of points: First don't use [*magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)). If you want to see if a character is a letter use the standard [`isalpha`](https://en.cppreference.com/w/c/string/byte/isalpha) function. And by modifying `ptr` you loose the original pointer returned by `malloc`, leading to a memory leak (which you have anyway since you don't call `free`). – Some programmer dude May 13 '19 at 12:24
  • 2
    You don't need to look at the input again after processing it, so you don't need to store it at all. Just read a single character with `getchar()`, process it and then discard it, i.e. overwrite it with the next char. – M Oehm May 13 '19 at 12:25
  • 2
    The problem statement is unclear. Check it again. BTW: What do you expect `*ptr++;` to do? – Support Ukraine May 13 '19 at 12:46
  • C doesn't have a magic string class, simple as that. If you are coming from higher level languages you might expect one to exist, but not so. – Lundin May 13 '19 at 13:23
  • In addition to the remarks already made, this part of your program also does not work: `x = *ptr; value = atoi(&x);`. The function `atoi` takes a pointer to a well-formed string. By passing the address of `x`, you make it access memory out of bounds looking for this terminating `'\0'` character. – Pascal Cuoq May 13 '19 at 13:26

1 Answers1

1

Here is error: char * ptr =(char *)malloc(sizeof(char *));

sizeof(char *) Equal 4 or 8. You allocated memory for example 8 chars. When you write entire STDIN to ptr then you will overwrite memory on heap.

When you read from stdin you MUST specify max length example:

char str[256] = {'\0'}
scanf ("%255s",str);     //Read max 255 chars

About your body of loop, this is better :)

uint8_t digit;
if (*ptr >= '0' && *ptr <= '9')
{
    digit = (uint8_t)(*ptr - '0');
    if (digit < 10)
    { 
        arr[digit]++;
    } 
}
ptr++;            //THERE IS NO STAR *

But you should use simple for with know length and

if (ptr[i] == '\0') break;

Hope it's helps.

Edit:

sizeof(char*) is 4 or 8 as suggested @dbush.

Igor Galczak
  • 142
  • 6
  • `sizeof(char *)` is not guaranteed to be 1. It is most likely to be either 4 or 8 depending on the size of a pointer on the system in question. But the point still stands that more data was read than can bit in the buffer. – dbush May 13 '19 at 14:28