How To Create The Reverse a String C++?

How to Reverse a String in C++ (8 Best Methods with Examples & Complexity)

Reversing a string in C++ means rearranging its characters so that the last character becomes first and the first becomes last.

Example:

Input:
Hello
Output:
olleH

Reversing strings is one of the most common beginner exercises and interview questions in C++. However, there are multiple ways to do it, each with different tradeoffs in performance, memory usage, and portability.

The best way to reverse a string in C++ is:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
string str = “Hello”;
reverse(str.begin(), str.end());
cout << str;
}

Time Complexity: O(n)

Space Complexity: O(1)

In-place reversal: Yes

This is the most efficient and production-ready solution.

Method 1: Using STL reverse() (Recommended)

The Standard Template Library provides reverse() in <algorithm>.

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
   string str = "Hello";
   reverse(str.begin(), str.end());
   cout << str;
}

Why this method is preferred:

  • Clean and readable
  • Optimized implementation
  • No extra memory allocation
  • Standard and portable

Method 2: Using Reverse Iterators (Constructor Method)

You can construct a reversed string using reverse iterators.

#include <iostream>
using namespace std;
int main() {
   string str = "Hello";
   string reversed(str.rbegin(), str.rend());
   cout << reversed;
}

Time Complexity: O(n)
Space Complexity: O(n)

This creates a new reversed string instead of modifying the original.

Method 3: Manual Loop with Temporary String

#include <iostream>
using namespace std;
int main() {
   string str = "Hello";
   string reversed;
   for(int i = str.length() - 1; i >= 0; i--) {
       reversed.push_back(str[i]);
   }
   cout << reversed;
}

Time Complexity: O(n)
Space Complexity: O(n)

Useful for understanding how string traversal works internally.

Method 4: Two Pointer In-Place Swap (Interview Favorite)

#include <iostream>
using namespace std;
int main() {
   string str = "Hello";
   int left = 0;
   int right = str.length() - 1;
   while(left < right) {
       swap(str[left], str[right]);
       left++;
       right--;
   }
   cout << str;
}

Time Complexity: O(n)
Space Complexity: O(1)

This is a very common interview solution because it demonstrates algorithmic thinking.

Method 5: Using Recursion

#include <iostream>
using namespace std;
void reverseString(string &str, int start, int end) {
   if(start >= end) return;
   swap(str[start], str[end]);
   reverseString(str, start + 1, end - 1);
}
int main() {
   string str = "Hello";
   reverseString(str, 0, str.length() - 1);
   cout << str;
}

Time Complexity: O(n)
Space Complexity: O(n) due to recursion stack

Good for demonstrating recursion but not memory efficient.

Method 6: Reversing a C-Style String (char array)

#include <iostream>
#include <cstring>
using namespace std;
int main() {
   char str[] = "Hello";
   int n = strlen(str);
   for(int i = 0; i < n / 2; i++) {
       swap(str[i], str[n - i - 1]);
   }
   cout << str;

}

Important Note:

strrev() is not part of the ISO C++ standard. It may work in some compilers but is not portable. Avoid using it in modern development.

Method 7: Using Stack

#include <iostream>
#include <stack>
using namespace std;
int main() {
   string str = "Hello";
   stack<char> s;
   for(char c : str)
       s.push(c);
   string reversed;
   while(!s.empty()) {
       reversed += s.top();
       s.pop();
   }
   cout << reversed;
}

Time Complexity: O(n)
Space Complexity: O(n)

Primarily educational for understanding stack behavior.

Method 8: Using C++20 ranges::reverse

If using C++20:
#include <iostream>
#include <ranges>
using namespace std;
int main() {
   string str = "Hello";
   ranges::reverse(str);
   cout << str;
}

Requires C++20 compiler support.

Best for modern C++ applications.

Complexity Comparison Table

Method Time Space In-Place
STL reverse O(n) O(1) Yes
Reverse iterators O(n) O(n) No
Manual loop O(n) O(n) No
Two pointer O(n) O(1) Yes
Recursion O(n) O(n) Yes
Stack O(n) O(n) No
C++20 ranges O(n) O(1) Yes

Best Overall: STL reverse()

Edge Cases to Consider

  1. Empty string
  2. Single character
  3. String with spaces
  4. Special characters
  5. Very large strings
  6. UTF-8 or multi-byte characters

Important:

Reversing UTF-8 strings byte by byte can corrupt multi-byte characters. For internationalized applications, use proper Unicode libraries instead of simple char reversal.

Common Interview Variations

  • Reverse words in a sentence
  • Check if string is palindrome
  • Reverse only vowels
  • Reverse without extra memory
  • Reverse linked list of characters

Being able to explain time and space complexity is critical.

Frequently Asked Questions

Q) What is the best way to reverse a string in C++?

A) Using STL reverse() because it is efficient, clean, and standard compliant.

Q) What is the time complexity of reverse() in C++?

A) O(n), where n is the length of the string.

Q) Is strrev() standard in C++?

A) No. It is not part of the ISO C++ standard and should be avoided.

Q) How do you reverse a string without STL?

A) Use the two-pointer swap method.

Q) How to reverse a char array in C++?

A) Use a loop swapping characters from both ends toward the center.

Q) Is recursion a good method for reversing a string?

A) It works but consumes additional stack memory, so it is not optimal for large strings.

Q) Can C++ safely reverse UTF-8 strings?

A) Not by default. You must use Unicode-aware libraries for safe reversal.

Final Conclusion

Reversing a string in C++ is a fundamental operation with multiple valid implementations. While manual loops and recursion help build algorithmic understanding, the STL reverse() function remains the best choice for real-world applications.

If you are preparing for interviews, master:

  • Two pointer approach
  • STL reverse
  • Complexity analysis

If you are writing production code, prioritize:

  • Portability
  • Standard compliance
  • Memory efficiency

With the methods, comparisons, and edge cases covered above, you now have a complete and modern understanding of string reversal in C++ suitable for both interviews and professional development.