C run time error. what's problem?


  • 0
    X
    char* reverseString(char* s) {
        int len = strlen(s);
    	char *temp = NULL;
    	temp = (char*)malloc(sizeof(char) * len); 
        int j = 0;
    	int i;
        for(i = len - 1; i >=0; i--){
            temp[j] = s[i];
    		j++;
        }
    	temp[j] = '\0';
    	return temp;
    }
    

  • 0

    @xpyZzuli This time I formatted your code for you...next time you'd better do it yourself if you are trying to post code here...using ``` to brace your code


  • 1

    @xpyZzuli there is a termination character after the string which the strlen will just ignore, so you have to allocate another byte for that character like below.

    char *temp = = (char*)malloc(sizeof(char) * (1+len)); 
    

Log in to reply
 

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