This is an interview question Looking for best optimal solution to trim multiple spaces from a string. This operation should be in-place operation.
input =
Here it is using only stdio:
#include int main(void){ char str[] = "I Like StackOverflow a lot"; int i, j = 0, lastSpace = 0; for(i = 0;str[i]; i++){ if(!lastSpace || str[i] != ' '){ str[j] = str[i]; j++; } lastSpace = (str[i] == ' '); } str[j] = 0; puts(str); return 0; }