Reverse the order of all characters inside the string : string reverse « String « C++






Reverse the order of all characters inside the string

  
 


#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    const string hello("Hello, how are you?");

    string s(hello.begin(),hello.end());

    // iterate through all of the characters
    string::iterator pos;
    for (pos = s.begin(); pos != s.end(); ++pos) {
        cout << *pos;
    }
    cout << endl;

    
    reverse (s.begin(), s.end());
    cout << "reverse:       " << s << endl;

}

/* 
Hello, how are you?
reverse:       ?uoy era woh ,olleH

 */        
    
  








Related examples in the same category

1.Reversing a String and Finding its Length
2.Print characters read from keyboard in reverse order
3.Reversing an STL String
4.Reverse a string in place.
5.Use a reverse iterator to display the string in reverse