Remove spaces from a string in C

Easiest and most efficient don’t usually go together…

Here’s a possible solution for in-place removal:

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

Leave a Comment