C++

How do you reverse a string in place in C or C

27 September 2026 · 5 min read

How do you reverse a string in place in C or C

Mastering string manipulation is a fundamental skill for any C or C++ developer. Among the various string operations, learning how to reverse a string in place in C or C++ is particularly valuable. This technique stands out because it modifies the original string directly without allocating additional memory for a new one. This approach is not only memory-efficient but also demonstrates a deep understanding of pointer manipulation and array handling, which are core concepts in these powerful languages. Whether you’re preparing for a coding interview, optimizing an existing application, or simply deepening your programming knowledge, understanding in-place string reversal is an essential step. We’ll explore the most common and efficient methods, focusing on the underlying logic and practical implementations in both C and C++ environments.

Understanding In-Place Reversal and C-Style Strings

The concept of “in-place” reversal means that you modify the data structure directly, using only a constant amount of extra memory (O(1) space complexity). This is distinct from creating a new, reversed copy of the string, which would require O(N) additional space, where N is the length of the string. In C and C++, strings are often represented as character arrays, which are sequences of characters terminated by a null character ('\0'). This null terminator is crucial because it marks the end of the string, allowing functions to know where the string data concludes.

Working with C-style strings (char or char[]) directly involves managing memory and pointers. Unlike higher-level languages that might abstract away string details, C and C++ require explicit handling of character arrays. When we talk about reversing a string in place, we’re typically referring to swapping characters within this existing character array. This method is highly optimized for performance and resource usage, making it a preferred solution in environments where memory is a concern or when dealing with very large strings.

For C++ specifically, while std::string objects are more convenient, they often manage their own internal character buffers. Performing an “in-place” reversal on a std::string usually means modifying its internal character sequence, which is still an in-place operation from the user’s perspective, even if the std::string class itself might handle memory allocation behind the scenes for resizing. However, the core logic remains the same: swapping characters from the ends towards the middle.

The Two-Pointer Approach: The Go-To Method

The most common and efficient algorithm to reverse a string in place involves a two-pointer approach. This method uses two pointers, one starting at the beginning of the string and the other at the end. These pointers then move towards each other, swapping the characters they point to at each step. This process continues until the pointers meet or cross, ensuring that every character has been swapped with its counterpart from the opposite end of the string. This elegant solution perfectly embodies the “in-place” principle, utilizing minimal additional memory.

For those looking to reverse a character array directly, the two-pointer approach offers a robust and straightforward solution. It ensures that the original memory allocated for the string is reused, making it an ideal choice for memory-constrained environments or performance-critical applications. The logic is simple yet powerful, making it a staple in any programmer’s toolkit for string manipulation.

To reverse a string in place in C or C++ using the two-pointer approach, initialize one pointer to the first character and another to the last non-null character. Repeatedly swap the characters pointed to by these pointers, then increment the start pointer and decrement the end pointer until the start pointer is no longer less than the end pointer. This process guarantees an efficient O(N) time complexity and O(1) space complexity.

Implementing the Two-Pointer Approach in C++

In C++, you can apply the two-pointer approach to either a C-style character array or an std::string object. For std::string, you can access its underlying character buffer or use iterators. The std::swap function from the header is particularly useful for cleanly exchanging character values.

  1. Initialize a left pointer (or index) to 0 and a right pointer (or index) to string_length - 1.
  2. Enter a loop that continues as long as left is less than right.
  3. Inside the loop, swap the characters at string[left] and string[right].
  4. Increment left by 1 and decrement right by 1.
  5. Once the loop finishes, the string will be reversed in place.

Here’s a C++ example demonstrating this with an std::string:

include <string> include <algorithm> // For std::swap include <iostream> void reverseString(std::string& s) { int left = 0; int right = s.length() - 1; while (left < right) { std::swap(s[left], s[right]); left++; right--; } } int main() { std::string myString = "hello"; reverseString(myString); std::cout << "Reversed string: " << myString << std::endl; // Output: olleh return 0; } 

Alternatively, C++ offers the std::reverse algorithm for std::string, which also performs an in-place reversal: std::reverse on cppreference.com. You can use it like this: std::reverse(s.begin(), s.end());.

Implementing the Two-Pointer Approach in C

For C, the process is largely the same, but you’ll work directly with char arrays and might need to implement your own swap function if you’re not using a utility like strcpy for character-by-character operations within a loop. Remember the importance of the null terminator; the length calculation should exclude it when determining the right pointer’s initial position, but the null terminator must remain at the end after reversal.

include <stdio.h> include <string.h> // For strlen // Custom swap function for characters void swap(char a, char b) { char temp = a; a = b; b = temp; } void reverseStringC(char str) { int length = strlen(str); int left = 0; int right = length - 1; // Exclude the null terminator while (left < right) { swap(&str[left], &str[right]); left++; right--; } } int main() { char myCString[] = "world"; reverseStringC(myCString); printf("Reversed C string
<b>Question & Answer : </b><br></br><p>How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?</p>
<br></br>#include <algorithm> std::reverse(str.begin(), str.end());  <p>This is the simplest way in C++.</p>