Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Recursion help (http://www.go4expert.com/forums/recursion-help-t1471/)

kingo 26Sep2006 11:30

Recursion help
 
I need to right a recursion program to reverse a string
the condition is
it should be only line
tht line code should be of returning the value

shabbir 26Sep2006 14:54

Re: Recursion help
 
Really interesting

Code:

#include <iostream.h>
#include <string.h>


void RevStr(char *inArr, char *outArr,int start,int cLen)
{
        ((*(outArr+start) = inArr[cLen-1]) && (cLen > 0))?RevStr(inArr, outArr, start+1, cLen-1):0;
}


int main()
{
        char * ptr = "Go4Expert";

        char *outptr = 0;

        outptr = new char[strlen(ptr) + 1];

        RevStr(ptr, outptr, 0, strlen(ptr));
 
        cout<<outptr;

        return 0;
}


kingo 26Sep2006 14:58

Re: Recursion help
 
I mean tht one line code should be of returning type.................I am not able to figure it out

shabbir 26Sep2006 15:26

Re: Recursion help
 
This one line is of returning type but as the function does not need to return anything and so it does not have a return statement.

Code:

((*(outArr+start) = inArr[cLen-1]) && (cLen > 0))?RevStr(inArr, outArr, start+1, cLen-1):0;

kingo 26Sep2006 16:21

Re: Recursion help
 
k thanx

shabbir 26Sep2006 16:39

Re: Recursion help
 
Quote:

Originally Posted by kingo
k thanx

My pleasure.

Also with the return statement heres the code.
Code:

#include <iostream.h>
#include <string.h>


int RevStr(char *inArr, char *outArr,int start,int cLen)
{
        return(((*(outArr+start) = inArr[cLen-1]) && (cLen > 0))?RevStr(inArr, outArr, start+1, cLen-1):0);
}


int main()
{
        char * ptr = "Go4Expert";

        char *outptr = 0;

        outptr = new char[strlen(ptr) + 1];

        RevStr(ptr, outptr, 0, strlen(ptr));
 
        cout<<outptr;

        return 0;
}



All times are GMT +5.5. The time now is 12:00.