How to Reverse a String in Place in C++? Last Updated : 13 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn how to reverse a string in place in C++. For Example,Input: myString = "Hello, World!" Output: myString = "!dlroW ,olleH"Reverse a String In-Place in C++ The std::reverse() function provided by the C++ STL library can be used to reverse the string in place. This function takes the iterator to the beginning and the end of the string. Syntax of std::reverse()std::reverse(begin, end) The begin and end can be either an iterator or a pointer. C++ Program to Reverse a String In-Place C++ // C++ program to reverse a string in place #include <algorithm> #include <iostream> #include <string> using namespace std; int main() { // Initialize the string string str = "Hello, World!"; // Reverse the string reverse(str.begin(), str.end()); // Output the result cout << "Reversed string is: " << str << endl; return 0; } OutputReversed string is: !dlroW ,olleH Time Complexity: O(n)Auxiliary Space: O(1) Note: In an in-place algorithm, additional space can be used as long as it is not dependent on the input. Comment More infoAdvertise with us Next Article Program to reverse words in a given string in C++ A anuragvbj79 Follow Improve Article Tags : C++ Programs C++ STL cpp-string CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read How to Reverse a List in C++ STL? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to reverse a list in C++. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Reversed List: 50 2 min read How to Reverse a Stack in C++? In C++, stacks are containers that store the elements in the last in-first out order(LIFO). In, this article, we will learn how we can reverse a stack in C++. Example Input: stack<int> S ={5,4,3,2,1} store Output: // Reversed Stack stack<int> S ={1,2,3,4,5}Reverse a Stack in C++We can re 2 min read Program to reverse words in a given string in C++ Given a sentence in the form of string str, the task is to reverse each word of the given sentence in C++. Examples: Input: str = "the sky is blue" Output: blue is sky theInput: str = "I love programming" Output: programming love I Method 1: Using STL functions Reverse the given string str using STL 6 min read C++ Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p 7 min read How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read Like