Last active
May 28, 2019 15:54
-
-
Save EdgeCaseBerg/5931082 to your computer and use it in GitHub Desktop.
Exercise 1-9 from The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie "Write a program to copy its input to its output replacing each string of one or more blanks by a single blank"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
main(){ | |
int c; | |
while((c = getchar()) != EOF){ | |
putchar(c); | |
if(c == ' '){ | |
while((c = getchar()) == ' ') | |
; | |
putchar(c); | |
} | |
} | |
//Use ctrl+D on unix systems to output an EOF to stdin | |
} | |
//symmetrical formatting |
that's an elegant solution!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you compile this with:
$ cc TheCProgrammingLanguage_Exercise1_9.c
Then run:
$ ./a.out < TheCProgrammingLanguage_Exercise1_9.c
You'll end up with the file outputted with extra spaces removed. Useful for a formatting tool.