Why does my C solution fail?


  • 0
    O

    Here's my code, it fails for the input:

    ""Revolt
    love!" raved Eva. "Revolt
    lover!""

    I would love to understand why. Tried to test it on many strings and it seemed to work.

    char* reverseString(char* s) {
        int len = strlen(s);
        char *new = malloc(len);
        for (int i = len - 1; i >= 0; i--)
        {
            *new++ = s[i];
        }
        *new = 0;
        return new - len;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.