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. Aug 26, 2011 at 2:12
  • @Benjamin: Nice, I was unaware of that.
    – Dave S
    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());
1
  • 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. Aug 26, 2011 at 2:48
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
  • 1
    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. Aug 26, 2011 at 1:54
  • Is x already a string? You can just say, return std::string(x.rbegin(), x.rend());.
    – Kerrek SB
    Aug 26, 2011 at 1:56
  • @Kerrek: Unlikely, since the stringstream is being used for, essentially, a lexical_cast. Aug 26, 2011 at 1:57
  • @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. Aug 26, 2011 at 2:06
4

Using std::reverse is easier.

std::reverse( source.begin(), source.end() ); // source is of type std::string
3
  • 2
    Note that it will reverse the string in-place, not create another string which is reversed. Aug 26, 2011 at 1:55
  • @Seth - Yes, that is the reason I didn't mention any return value.
    – Mahesh
    Aug 26, 2011 at 1:55
  • 6
    yeah I know you know, but I don't know he knows :) Just mentioning it for the sake of avoiding ambiguities and misunderstandings. Aug 26, 2011 at 1:58
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
  • Glorious, thanks for the help, this question was so simple that it eluded me for the better part of 20 minutes, thanks! Aug 26, 2011 at 1:53
  • 2
    @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. Aug 26, 2011 at 1:54
  • @Hundering- I wrote this answer and don't want you to accept it... you should definitely use another approach! Aug 26, 2011 at 2:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.