To reverse a string in C, swap the first and last characters, then the second and the second to last, and continue towards the middle until the two indexes meet. This in-place approach needs no extra memory and runs in linear time. It is a classic interview question, so this guide on how to reverse a string in C shows the two-pointer version, a recursive version and the edge cases interviewers ask about.
The key facts are that a string is a char array ended by '\0', that you only swap the characters before the terminator, and that the string must be modifiable.
Find the length with strlen, put one index at the start and one at the last character, and swap while the left index is smaller than the right one.
The early return for len < 2 matters. Because len is an unsigned size_t, len - 1 on an empty string wraps around to a huge number, and the loop would run off the array. Checking the length first avoids that bug, which is a favourite interview trap.
Each character is touched once, so the time is O(n) and the extra space is O(1).
The function writes into the string, so you cannot pass a pointer to a string literal.
Use an array initialised from the literal, or copy the text into a heap buffer first. This is the same rule that applies to every function that changes a string.
When the original must stay unchanged, write the reversed text into a second array with room for the terminator.
The destination must hold len + 1 bytes, and the explicit dest[len] = '\0' is essential, because the loop only copies the visible characters.
Recursion is a common follow-up question. The function swaps the outer pair and calls itself on the inside part.
It uses O(n) stack space because each call adds a frame, so the iterative form is better for long strings.
| Approach | Time | Extra space | Changes original |
|---|---|---|---|
| Two indexes, in place | O(n) | O(1) | Yes |
| Copy to new buffer | O(n) | O(n) | No |
| Recursive | O(n) | O(n) stack | Yes |
Test these inputs: an empty string, a single character, an even length, an odd length and a string with spaces. All should work with the code above.
The function reverses bytes, not characters. For plain ASCII that is correct. In UTF-8 text a character such as the letter e with an accent can use two bytes, and reversing the bytes produces invalid text. Handling this needs a UTF-8 aware routine or a library, which is beyond a simple exercise, but you should mention it in an interview.
Yes, with an XOR swap, but it is no faster on modern compilers and is less readable, and it fails if both operands are the same object. A normal temporary variable is the better choice.
No. Standard C has no strrev; some Windows compilers provide a non-standard _strrev, so write your own for portable code.
It is O(n), because each character is read once and swapped at most once, and half as many swaps as characters are needed.
The terminator was probably misplaced or the buffer was too small. Make sure the null stays at index len and is never swapped.
Reverse a string in C by swapping characters from both ends until the indexes meet, guarding against empty strings and keeping the terminator in place. The in-place method is O(n) time and O(1) space, requires a modifiable array, and works on bytes rather than Unicode characters.