Reversing a string in c with recursion

后端 未结 11 759
遥遥无期
遥遥无期 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:32

    #include
    #include
    void rev(char *);
    
    int main()
    {
        char s[]="Hello";
        printf("\n%s",s);
        rev(s);
        printf("\n%s",s);
        return 0;
    }
    
    void rev(char *s)
    {
        static int i=0;
        static int j=0;
        if(j==0)                   //since static variable can be intitialized 
        {                          //only with a constant literal,to store 
            j=strlen(s)-1;         //length-1 in 1st function call, we can use 
        }                          //this trick.(condition satisfied only in 1st 
                                   //function call)
        if(i

提交回复
热议问题