Stuck on "race car"


  • 0
    C

    This is my code for the reverse string problem. I can't figure out why it does not pass the test case for "race car". When I use race car as a custom input and select show difference there is nothing being shown. Can anyone spot the problem?

    char* reverseString(char* s) {

    int length = strlen(s);
    char* rs;
    
    rs = (char*) malloc(length*(sizeof(char)));
    
    //printf("length = %d", length);
    
    //int space = strstr(s, ' ');        
    //printf("space %d", space);
    
    for(int start = 0, end = (length - 1); end >= 0; start++, end--){
        
        *(rs + start) = *(s + end);
    
        //printf("s %c %d e %c %d\n", *(rs + start), start, *(s + end), end);
    }
    
    //printf("%s", rs);
    
    
    return rs;
    

    }


  • 0
    K

    I had the same issue. I think you might need rs[length] = '\0' to signify the end of the string. You will have to change in the malloc line to be (length+1) for that extra char.


Log in to reply
 

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