What if there is a const before char* s


  • 0
    M

    like this

    char* reverseString(const char* s){
    }
    
    char* reverseString(const char* s){
    if(!string) return (char*)"";
        size_t len = strlen(string);
        char* c = (char*)malloc(len + 1);
        //char* c = new char[len];
        for(size_t i = 0; i<len; i++){
            c[i] = string[len - i - 1];
        }
        return c;
    }
    

    I tired this but some test cases not passed, can anyone tell me what the problem is?


  • 0
    M

    @MichaelWayne said in What if there is a const before char* s:

    like this

    char* reverseString(const char* s){
    }
    
    char* reverseString(const char* s){
    if(!string) return (char*)"";
        size_t len = strlen(string);
        char* c = (char*)malloc(len + 1);
        //char* c = new char[len];
        for(size_t i = 0; i<len; i++){
            c[i] = string[len - i - 1];
        }
        return c;
    }
    

    I tired this but some test cases not passed, can anyone tell me what the problem is?

    besides there should be string in the declaration


Log in to reply
 

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