Reversing a string in C++ using a reverse iterator?

16,796

Solution 1

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());

Solution 2

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!

Solution 3

Using std::reverse is easier.

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

Solution 4

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;
}
Share:
16,796
HunderingThooves
Author by

HunderingThooves

Updated on June 11, 2022

Comments

  • HunderingThooves
    HunderingThooves almost 2 years

    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!

  • HunderingThooves
    HunderingThooves over 12 years
    Glorious, thanks for the help, this question was so simple that it eluded me for the better part of 20 minutes, thanks!
  • Seth Carnegie
    Seth Carnegie over 12 years
    @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.
  • HunderingThooves
    HunderingThooves over 12 years
    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.
  • Seth Carnegie
    Seth Carnegie over 12 years
    Note that it will reverse the string in-place, not create another string which is reversed.
  • Mahesh
    Mahesh over 12 years
    @Seth - Yes, that is the reason I didn't mention any return value.
  • Kerrek SB
    Kerrek SB over 12 years
    Is x already a string? You can just say, return std::string(x.rbegin(), x.rend());.
  • C. K. Young
    C. K. Young over 12 years
    @Kerrek: Unlikely, since the stringstream is being used for, essentially, a lexical_cast.
  • Seth Carnegie
    Seth Carnegie over 12 years
    yeah I know you know, but I don't know he knows :) Just mentioning it for the sake of avoiding ambiguities and misunderstandings.
  • templatetypedef
    templatetypedef over 12 years
    @Hundering- I wrote this answer and don't want you to accept it... you should definitely use another approach!
  • HunderingThooves
    HunderingThooves over 12 years
    @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.
  • HunderingThooves
    HunderingThooves over 12 years
    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.