C solution using recursion


  • 0
    L
    inline void swap_char(char* a,char* b){
        char c = *a;
        *a = *b;
        *b = c;
    }
    inline void swap_str(char* s,int k){
        for (char *i=s,*j=s+k-1;i<j;i++,j--){
            swap_char(i,j);
        }
    }
    void reverseStr_len(char* s, int k,int str_len) {
        if(str_len<=1){
            return;
        }else if(str_len < k){
            k = str_len;
        }
        swap_str(s,k);
        reverseStr_len(s+2*k,k,str_len-2*k);
    }
    char* reverseStr(char* s, int k) {
        int str_len = strlen(s);
        //printf("str_len:%d\r\n",str_len);
        reverseStr_len(s,k,str_len);
        return s;
    }
    

Log in to reply
 

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