Reversing a string in c with recursion

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

    use sprintf it will print your reversed string into buffer.

    #include
    
    char *b;
    
    main()
    {
      char a[17]="abcdefg";
      char buffer[17];
      buffer[0]= '\0';
      b = buffer;
      reverse(a);
      printf("%s\n",buffer);
    }
    int reverse(char *a)
    {
       if(*a!='\0')
       {   
         reverse(a+1);
         sprintf(b,"%c",*a);
         b++;
       }
    
    }
    

提交回复
热议问题