Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Created July 5, 2013 01:55
Show Gist options
  • Save EdgeCaseBerg/5931197 to your computer and use it in GitHub Desktop.
Save EdgeCaseBerg/5931197 to your computer and use it in GitHub Desktop.
Simple program to take an input from the stdin and spit it to stdout on a single line without any duplicate whitespace. Not as efficient as it could be, but quick and dirty.
#include <stdio.h>
main(){
int c;
while((c = getchar()) != EOF){
if(c == '\n'){
putchar(' ');
while((c = getchar()) == '\n')
;
if(c != ' ' && c != '\t')
putchar(c);
continue;
}
if(c == '\t'){
putchar(' ');
while((c = getchar()) == '\t')
;
if(c != ' ' && c != '\n')
putchar(c);
continue;
}
putchar(c);
if(c == ' '){
while((c = getchar()) == ' ')
;
putchar(c);
}
}
//Move curser down
printf("\n");
//Use ctrl+D on unix systems to output an EOF to stdin
}
@EdgeCaseBerg
Copy link
Author

To run it:

$cc onelineit.c -o onelineit
$./onelineit < inputfile.txt > output.txt

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