How do you reverse a string in place?
Short answer: Find the length, then swap characters from both ends moving inward, stopping when the indices meet. Do not swap the '\0' terminator. O(n) time, O(1) space.
#include <string.h>
void reverse_string(char *s)
{
size_t len = strlen(s);
if (len < 2) return;
size_t low = 0, high = len - 1; /* last real character, not '\0' */
while (low < high) {
char t = s[low];
s[low] = s[high];
s[high] = t;
low++;
high--;
}
}
/* "hello world" -> "dlrow olleh" */
Do not hard-code high = 10. It works only for an 11-character string. Compute it from the length.
It must be writable memory.
char a[] = "hello"; /* array copy on the stack: reversing is fine */
char *p = "hello"; /* points to a string literal: writing is undefined behaviour */
On a desktop the second one usually crashes (literals are in a read-only section). On a microcontroller the literal is in flash and the write silently does nothing or raises a fault.
Recursive version (a common follow-up), O(n) stack depth:
void reverse_rec(char *s, size_t lo, size_t hi)
{
if (lo >= hi) return;
char t = s[lo]; s[lo] = s[hi]; s[hi] = t;
reverse_rec(s, lo + 1, hi - 1);
}
UTF-8 caveat: reversing bytes breaks multi-byte characters such as "é" (two bytes). Reversing by code point requires decoding first. Mention it if the interviewer asks about real-world text.