Hello,
I need a function to replace multiple spaces with only one.
I wrote this one but if it happens that the string starts with one or more spaces then the function will leave one there, like in this example:
I'm aware of the fact that there are multiple ways to remove the first character of a string but I'd like to do this in the replace_spaces function instead of using something like memmove/strlenCode:#include <stdio.h> #include <stdlib.h> #include <ctype.h> void replace_spaces(char *s) { char *p = s; while (*s != '\0') { while (isspace(*s) && isspace(*(s + 1))) s++; *p++ = *s++; } *p = '\0'; } int main(void) { char input[] = " aaaa bbbb "; replace_spaces(input); puts(input); return EXIT_SUCCESS; }