Reversing a string in c with recursion

后端 未结 11 862
遥遥无期
遥遥无期 2021-01-22 09:47

I have written code to reverse a string in c... it works fine but I can\'t return the reversed string in the main() function.

#include

        
11条回答
  •  庸人自扰
    2021-01-22 10:08

    #include 
    #define MAX 100
    int main()
    {
    char str[MAX], *rev;
    scanf("%s", str);
    rev = reverse(str); 
    printf("The reversed string is : %s\n", rev);
    return 0;
    }
    char *reverse(char ch[])
       {
        static char r[MAX];
        static int i=0;
        if(*ch == '\0') return "";
        else 
       {
        reverse(ch+1);
        r[i++]=*ch;
       }
        return r;
       }
    

提交回复
热议问题