原文: https://beginnersbook.com/2014/06/c-program-to-reverse-a-string-using-recursion/

    这里我们定义了一个函数reverse_string,这个函数递归地调用它自己。

    1. #include <stdio.h>
    2. #include <string.h>
    3. void reverse_string(char*, int, int);
    4. int main()
    5. {
    6. //This array would hold the string upto 150 char
    7. char string_array[150];
    8. printf("Enter any string:");
    9. scanf("%s", &string_array);
    10. //Calling our user defined function
    11. reverse_string(string_array, 0, strlen(string_array)-1);
    12. printf("\nReversed String is: %s",string_array);
    13. return 0;
    14. }
    15. void reverse_string(char *x, int start, int end)
    16. {
    17. char ch;
    18. if (start >= end)
    19. return;
    20. ch = *(x+start);
    21. *(x+start) = *(x+end);
    22. *(x+end) = ch;
    23. //Function calling itself: Recursion
    24. reverse_string(x, ++start, --end);
    25. }

    输出:

    1. Enter any string: chaitanya
    2. Reversed String is: aynatiahc