9p / who / tweedy / 9C / 1


Exercise 1.9: Squeeze blanks

Code

#include <u.h>
#include <libc.h>
#include <bio.h>

#define STDIN   0
#define STDOUT  1

/* squeeze blanks (remove multiples)     */

void
main()
{
    Biobuf *bstdin, *bstdout;
    bstdin = Bfdopen(STDIN, OREAD);
    bstdout = Bfdopen(STDOUT, OWRITE);
    char c, prev;

    prev = 'x';     /* this can be any non-blank character */
    c = Bgetc(bstdin);
    while(c >= 0){
        if(!(c == ' ' && prev == ' '))
            Bputc(bstdout, c);
        Bflush(bstdout);
        prev = c;
        c = Bgetc(bstdin);
    }

exits(0);
}

Output

$ 9c squeeze.c; 9l squeeze.o -o squeeze
$ ./squeeze 
   hello
 hello
hello     world
hello world



tweedy