How to Reverse a String in C++?

Last Updated : 31 Mar, 2026

Reversing a string means swapping 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++. For Example:

Input: str = "Hello World"
Output: dlroW olleH

Explanation: The last character is swapping by first character, second last character by second character and so on.

Below are some methods of reversing a string:

Using reverse() method

C++ STL provides the std::reverse() method that reverses the order of elements in the given range. We can use this function to reverse the string by passing the iterators to the beginning and the end of the string. This method is defined inside <algorithm> header file.

Syntax

std::reverse(str.begin(), str.end());

where str is the string to be reversed.

Code Implementation:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    string str = "Hello World";

    // Reverse the string using the std::reverse() 
    reverse(str.begin(), str.end());

    cout << str;
    return 0;
}

Output
dlroW olleH

Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(1)

There are a few more methods to reverse the string in C++. Please refer this article - Different Methods to Reverse a String.

Comment