Introduction
Reversing a string is one of the most common interview and coding test questions in C++ programming. The goal is simple — you are given a string, and you must reverse its characters so that the first becomes last, the second becomes second last, and so on.
However, the challenge here is to reverse the string in-place, meaning you should not use any extra memory (like creating another string or array). Instead, you modify the existing string directly in memory.
What Does In-Place Mean?
“In-place” means that we do not use extra data structures like another array, vector, or string to store the reversed result. The original string’s characters are rearranged within the same memory space.
This saves memory and improves efficiency — especially important for large strings or memory-constrained environments like embedded systems.
Logic Behind Reversing a String In-Place
The idea is simple:
Use two pointers — one starting from the beginning (start), another from the end (end).
Swap the characters at these positions.
Move both pointers toward the center.
Continue until start becomes greater than or equal to end.
This method modifies the string directly, with O(1) extra memory and O(n) time complexity.
Step-by-Step Implementation
Let’s look at the C++ code and break it down.
Example 1: Using Two Pointers
#include <iostream>
#include <string>
using namespace std;
void reverseString(string &str) {
int start = 0;
int end = str.length() - 1;
while (start < end) {
// Swap characters using a temporary variable
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int main() {
string text = "Hello, World!";
reverseString(text);
cout << "Reversed String: " << text << endl;
return 0;
}
Output:
Reversed String: !dlroW ,olleH
✅ Explanation:
string &str — passed by reference to modify the original string.
start and end represent the positions of characters to swap.
The loop continues until all characters are reversed.
Example 2: Using std::swap() Function
C++ provides a built-in swap() function in the <utility> header, making the code cleaner.
#include <iostream>
#include <string>
#include <utility>
using namespace std;
void reverseString(string &str) {
int start = 0;
int end = str.length() - 1;
while (start < end) {
swap(str[start], str[end]);
start++;
end--;
}
}
int main() {
string s = "OpenAI GPT";
reverseString(s);
cout << "Reversed: " << s << endl;
return 0;
}
Output:
Reversed: TPG IAnepO
✅ Key Points:
Example 3: Using C++ STL reverse() Function
C++ provides a handy reverse() function from the <algorithm> library, which can reverse a string efficiently in-place.
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string word = "CSharpCorner";
reverse(word.begin(), word.end());
cout << "Reversed String: " << word << endl;
return 0;
}
Output:
Reversed String: renroCphSraC
✅ Explanation:
reverse() takes two iterators — beginning and end.
It performs the same swapping logic internally.
The operation is still in-place and does not require extra memory.
Time and Space Complexity
| Operation Type | Complexity | Description |
|---|
| Time | O(n) | Each character is swapped once. |
| Space | O(1) | Reversal happens within the same string memory. |
Edge Cases to Consider
Empty String → Return the same string.
Single Character → No change.
Palindromes → The reversed string is identical to the original.
Strings with Spaces or Symbols → Handle them as regular characters.
Example:
string s = "madam"; // palindrome
reverseString(s);
cout << s; // Output: madam
Alternative: Reversing C-Style Strings (Character Arrays)
In older C++ programs, you may work with C-style strings (char arrays). Here’s how to reverse them in-place:
#include <iostream>
#include <cstring>
using namespace std;
void reverseCString(char str[]) {
int start = 0;
int end = strlen(str) - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int main() {
char name[] = "Python";
reverseCString(name);
cout << "Reversed: " << name << endl;
return 0;
}
Output:
Reversed: nohtyP
✅ Works in-place even for C-style strings.
Summary
Reversing a string in-place in C++ is simple and memory-efficient. The two-pointer technique and built-in reverse() method make it easy to achieve without allocating extra space.
Key Takeaways:
Use the two-pointer method for manual control.
Use std::swap() for cleaner syntax.
Use std::reverse() for a one-line STL solution.
All methods work in-place and have O(n) time, O(1) space complexity.
By mastering this small but essential concept, you strengthen your understanding of C++ strings, memory optimization, and in-place algorithms — vital skills for coding interviews and real-world programming.