Ad
  • Custom User Avatar

    Cannot allocate the string statically in C,free(): invalid pointer.

    Please indicate so in the description.

  • Custom User Avatar

    Reading the top comment, I'm wondering if this is a joke.

  • Custom User Avatar

    The idea of using a state machine is a good one, but I think it'd be a better design to only give it one job: get the right coordinates.

    Accessing the array with the coordinates should be done outside of this state machine.

  • Custom User Avatar

    The idea of using a state machine is a good one, but yours is way too over-engineered.

  • Custom User Avatar

    Ok so I figured it out. I thought that the function expected outsz as the output, but it simply expects you to set it, and to allocate your own array and return it.

    I think this could be made a lot clearer.

  • Custom User Avatar

    The C translation seems to be bugged, I am unsure what output is expected.

    Accessing the same element [0][0] of mx in a loop produces undefined behavior.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Using the result of a comparison, very clever. I love that.

  • Custom User Avatar

    I think that's my favorite solution so far.

  • Custom User Avatar

    Your tenary operations are difficult to read (though clever), please add some spaces.

  • Custom User Avatar

    I think you should use better variable names. n means nothing.

  • Custom User Avatar

    If you increment the pointer s at every loop and dereference it until its value is '\0', you don't need length, i or the function strlen, saving you space, performance and readability.
    Though, if you do that, you will need to ensure that min is greater than length by the first comparison. Simply declare length an unsigned int (you can also declare and initialize min while you're at it), and set the the value to ~0, which sets it at the highest possible value for an integer. This operation is very cheap on the CPU, since you're really only flipping bits, and it guarantees the that the first comparison between temp and min works as intended.

  • Custom User Avatar

    You can use brackets even on non-arrays. sentence[1] and *(sentence + 1) are equivalent.

    Aside from that, your idea of synchronizing sentence and result, to edit a custom array temp and of copying temp over result is way more effective than I would have thought. I am genuinely impressed, this is clever.

  • Custom User Avatar

    I think that you're being too liberal with the variety of tools that you use. sprintf, strcpy, memset, the list goes on. You're also using manual pointers.

    There exist simpler and safer ways of achieving the desired result.

  • Custom User Avatar

    Try to not overuse single-letter variables that way, all declared at the top. It makes it very difficult to understand the program.
    Also, instead of writing everything into a scoped if statement, you could write

    if (!isalpha(result[i])) continue;

    And save some precious horizontal and mental space.

  • Loading more items...