data-structures · intermediate · ~25 min
Compose two reversals into a non-trivial rearrangement.
Given a writable C-string s of words separated by single spaces (no leading/trailing whitespace), reverse the order of the words in place. Implement void reverse_words(char *s).
The 'reverse whole, then reverse each word' technique is a classic interview question and shows up in real text-processing pipelines that need O(1) memory.
Null-terminated string with words separated by single spaces.
Same buffer, words reversed.
O(1) auxiliary memory. No strdup, no malloc.
void reverse_words(char *s) { /* TODO */ }
Reversing characters byte-by-byte and forgetting to also reverse each word; mishandling empty input; using strtok which mutates and loses span info.
Empty string. One word. Two words.
O(n) time, O(1) memory.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.