7

I have the following code and I just can't seem to figure out a way to get the strings reversed here:

stringstream convert;
string y="";
string z="";
convert << x;
string::reverse_iterator rit;
y=convert.str();
int j=0;
for (rit = y.rbegin(); rit < y.rend(); rit++){
    z[j] = *rit;
    j++;
}

Can someone help me out with this? Thanks!

2
  • 2
    Sidenote. If your intent is to convert numbers to strings, you don't(may not) need stringstreams or boost or sprintf anymore. C++11 has a to_string function overloaded for all sorts of numeric types. Commented Aug 26, 2011 at 2:12
  • @Benjamin: Nice, I was unaware of that. Commented Aug 26, 2011 at 2:44

4 Answers 4

20
z.assign(y.rbegin(), y.rend());

Or you can do it upon construction:

std::string z(y.rbegin(), y.rend());

If you want to modify a string in place, use std::reverse:

std::reverse(y.begin(), y.end());
Sign up to request clarification or add additional context in comments.

1 Comment

I decided to go with this one because of the great amount of explanation offered here. I was really torn between this one Chris Jester-Young below since you both offered excellent answers.
8

I'd do this:

stringstream convert;
convert << x;
string y(convert.str());
string z(y.rbegin(), y.rend());
return z;

No need to write a manual loop!

4 Comments

This is also an excellent answer, I think that despite Templatetypedef beating you by a matter of seconds that I'll end up accepting this one for the sake of code optimization.
Is x already a string? You can just say, return std::string(x.rbegin(), x.rend());.
@Kerrek: Unlikely, since the stringstream is being used for, essentially, a lexical_cast.
@Chris -- Correct, I'm using stringstream since I haven't yet delved into the Boost libraries, The function the code above appears in actually returns a bool already, so I couldn't just return it like that.
4

Using std::reverse is easier.

std::reverse( source.begin(), source.end() ); // source is of type std::string

3 Comments

Note that it will reverse the string in-place, not create another string which is reversed.
@Seth - Yes, that is the reason I didn't mention any return value.
yeah I know you know, but I don't know he knows :) Just mentioning it for the sake of avoiding ambiguities and misunderstandings.
2

I think that your problem is in this loop:

int j=0;
for (rit = y.rbegin(); rit < y.rend(); rit++){
    z[j] = *rit;
    j++;
}

Notice that you're writing into the string z at various positions. However, you haven't actually initialized z so that there's any elements in it, so this is writing to nonexistent locations, which results in undefined behavior.

To fix this, instead of writing to locations in z, try appending new characters to the end:

for (rit = y.rbegin(); rit < y.rend(); rit++){
    z += *rit;
}

3 Comments

Glorious, thanks for the help, this question was so simple that it eluded me for the better part of 20 minutes, thanks!
@Hundering while this answer is correct, you probably shouldn't do it this way because it's neither concise nor efficient. Look at some of the other answers for better ways.
@Hundering- I wrote this answer and don't want you to accept it... you should definitely use another approach!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.