Ad
  • Default User Avatar

    I really like the idea of this solution; scanning the data pointer around the edge then moving to the inner edge! Seems like everyone else, myself included, did calculations to find the next element to copy instead of just moving it around. This is much easier to read, I would imagine.

  • Custom User Avatar

    I think if the result has all 26 letters then you'd better calloc 27 (not 26) to make room for the \0, otherwise ...

  • Default User Avatar

    you too!

  • Custom User Avatar

    Almost identical to mine. As well as the OB1 problem (the final string might be of 27 bytes including the null terminator), there's a second issue: overflow in letters.

    while(*s1) letters[*s1++] = 1;
    

    won't suffer from this.

  • Custom User Avatar

    @caelumable, temp is merely a pointer. It points into same chunk of calloced memory that final points at.

    That memory will be freed in the test code.

    EDIT: My mistake, the test code doesn't free the returned memory at all. I guess the author wrote code in this way to allow solutions this return references to static variables.

  • Default User Avatar

    eum....may I ask when dose the temp be destroyed? and why ? the temp didn't declear in the domain of the for loop, why it destroyed after the for loop?

  • Custom User Avatar

    it doesn't matter if he use sizeof(char), as this would only consume compile time and it would become a constant.

  • Default User Avatar

    temp pointer is incremented in a for loop, if you want to return it you should decrement it same amount of times because, as @status stated, it points to nothing after for loop.

  • Default User Avatar

    Because temp pointer point to nothing now.

  • Default User Avatar

    Because temp pointer point to nothing now.

  • Default User Avatar

    Why not return temp directly?
    Why are you returning final?

  • Custom User Avatar

    Nice solution. Some nitpicks:

    1. I see this a lot here, but sizeof(char) is by definition always 1, so it's kinda redundant to write that instead of just 1.
    2. Also for best practice, you should always check the return value of malloc, calloc and friends and null terminate strings.
    3. And you could realloc(final, temp - final) in the end so you don't return a buffer that is longer than it needs to be.
  • Custom User Avatar
  • Custom User Avatar

    Damn, nice solution.

  • Default User Avatar

    My code was ridiculously similar to yours, haha.

  • Loading more items...