Hi,Can somebody write a sample code to reverse a string using recursion.Basically a function which takes string as input parameter, reverses the string using recusrion and returns back the reversed string to calling function.I have written this code:#include void rev_str(char* s){if(*s != '')rev_str(s+1);printf("%c",*s);}int main(){rev_str("born2c0de");return 0;}Here i am able to reverse the string and print it out...but i want rev_str function to return the reversed string or ideally reverse the passed string itself.Can you help me out?

Questions by varun51

  
Showing Answers 1 - 3 of 3 Answers

G Siva Prakash Reddy

  • Oct 18th, 2007
 

#include<stdio.h>
#include<string.h>

main( int argc, char ** argv )
{
char *reversedString;
                  if( argc == 1 )
                  {
                         printf("n INCORRECT USAGE n");
                         return -1;
                   }

                   strReverseRecursive( argv[2], reversedString );
                    printf("n reversed string :: %s n", reversedString );
}

char *strReverseRecursive( char *revStr,char *revString )
{
static int i = 0;
int len = 0;
        if ( *revStr == '' )
            return;
   strReverseRecursive( ++revStr,revString );
        revString[i]=*--revStr;
        i++;
return revStr;
}


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions