C/C++ Arena

Step 10 of 10

Challenge: squeeze out spaces

Another two-pointer classic: a read pointer scans every character, while a write pointer only advances when a character is kept. Everything happens in the same buffer, no extra memory.

"a b  c"      read ->  a _ b _ _ c
"abc"         write -> a b c \0

Don't forget to write the '\0' terminator at the final write position.

Your turn: write void squeeze(char *s) that removes every space from s in place.

Previous: Two pointers from both ends