fork download
  1. #include <stdio.h>
  2.  
  3. char *merge_spaces(char *str) {
  4. char *saved_str, *pos;
  5. saved_str = pos = str;
  6. bool prev_was_ch = false;
  7. while (*str) {
  8. if (*str != ' ' || prev_was_ch) {
  9. *pos++ = *str;
  10. prev_was_ch = *str != ' ';
  11. }
  12. str++;
  13. }
  14. pos[-(!prev_was_ch && pos != saved_str)] = '\0';
  15. return saved_str;
  16. }
  17.  
  18. int main(void) {
  19. char str[] = " hello world this is a test ";
  20. printf("[%s]\n", merge_spaces(str));
  21. return 0;
  22. }
  23.  
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
[hello world this is a test]