Depends on how you are using the function. I look at it, and I don't see it reversing anything... So I'm not really sure what the function should be doing.
But, if you want it to return a pointer, then just make it return a pointer.
1 2 3 4 5 6 7
string * r_reverse(string originalString, int index)
{
if ( index < 0 )
return originalString.at(0);
elsereturn originalString.at(index) + r_reverse(originalString, index - 1 );
}
Now it returns a pointer to a string. HOWEVER, at() returns a character, so you should be getting a conversion error.
It actually is reversing: I just changed the at() like you said with substr(index,1)
so to use:
cout << r_reverse("word.", 4) << endl;
You have to indicate to start at last position and I realized it should be:
1 2 3 4 5 6 7 8
string r_reverse(string originalString, int index)//NB: index is current last
// position in original string
{
if ( index < 0 )
return"";//NOT return originalString.substr(index,1);
elsereturn originalString.substr(index,1) + r_reverse(originalString, index - 1 );
}
Well of course not. That's how you declare a function returning a pointer. You need to make the actual code inside the function return an actual pointer to get it to work.
Ah ok. Then the way you are doing it cannot and should not return a pointer to a string, as there is no reason. However, it IS return a pointer to a character array (which is what a string basically is).