Removing Spaces from a String in C?

Tyler Treat picture Tyler Treat · Nov 13, 2009 · Viewed 158.2k times · Source

What is the easiest and most efficient way to remove spaces from a string in C?

Answer

Aaron picture Aaron · Nov 13, 2009

Easiest and most efficient don't usually go together...

Here's a possible solution:

void remove_spaces(char* s) {
    const char* d = s;
    do {
        while (*d == ' ') {
            ++d;
        }
    } while (*s++ = *d++);
}