Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Last active May 28, 2019 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdgeCaseBerg/5931082 to your computer and use it in GitHub Desktop.
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"
#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
@EdgeCaseBerg
Copy link
Author

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.

@bailtree
Copy link

that's an elegant solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment