reverse string help

closed account (4ET0pfjN)
hi, it's not compiling for my function:

1
2
3
4
5
6
7
8
string r_reverse(string originalString, int index)
{
	
	if ( index < 0 )
		return originalString.at(0);
	else
		return originalString.at(index) + r_reverse(originalString, index - 1 );
}
at() returns a char type, not a string type.
closed account (4ET0pfjN)
also, while we're at it, how would i make the same function but it returns a pointer by taking in a string array?
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);
	else
		return 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.
closed account (4ET0pfjN)
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);
	else
		return originalString.substr(index,1) + r_reverse(originalString, index - 1 );
}
closed account (4ET0pfjN)
It doesn't work btw with just appending * infront of function name...
Then good job! You answered your own question!
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.
Last edited on
Also. I don't see any reason in returning a pointer. Can you state your reason for wanting this?
closed account (4ET0pfjN)
for the sake of learning :)
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).
This is how I would simplify your function:
1
2
3
4
5
6
7
8
9
10
11
string r_reverse ( string originalString )
{
    if ( originalString.size ( ) == 0 )
    {
        return "";
    }
    else
    {
        return originalString[originalString.size()-1] + r_reverse ( originalString.substr ( 0, originalString.size ( ) - 1 ) );
    }
}


Now we don't have to worry how long the string is.
Pass by const reference, no need for the copy:

1
2
3
4
5
6
string r_reverse ( const string &originalString )
{
    if(originalString.empty())
        return "";
    return originalString[originalString.size()-1] + r_reverse ( originalString.substr ( 0, originalString.size ( ) - 1 ) );
}
Awesome. I was wondering what that error meant. Thanks for the info, clan!
Topic archived. No new replies allowed.