To reverse a string "in place" without using a temporary string, use the reverse function template in the <algorithm> header: - C++ STL

C++ examples for STL:string

Description

To reverse a string "in place" without using a temporary string, use the reverse function template in the <algorithm> header:

Demo Code

#include <string>
#include <iostream>

using namespace std;

int main() {// w w w  .  ja v  a 2s  .co  m

   string s = "this is a test";

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

   cout << s;
}

Result


Related Tutorials