Reverse a string using a recursive function

后端 未结 5 1870
悲哀的现实
悲哀的现实 2021-01-22 09:10

I am currently studying C and I can\'t get past this exercise. I must create a recursive function to reverse string1 into string2. Here is my code. I w

5条回答
  •  星月不相逢
    2021-01-22 09:59

    Although it does not save the resulting string anywhere, you get the idea.

    #include 
    
    void rev (const char* str);
    
    int main () {
        const char str[] = "!dlrow ,olleH";
    
        printf("%s\n", str);
    
        rev(str);
        printf("\n");
    
        return 0;
    }
    
    void rev (const char* str) {
        char c = *str;
        if (c != '\0') {
                rev(str + 1);
            printf("%c", c);
        }
    }
    

提交回复
热议问题