9p / who / tweedy / 9C / 1
Exercise 1.12: Print each word on a new line
Code
#include <u.h>
#include <libc.h>
#include <bio.h>
#define STDIN 0
#define STDOUT 1
/* print each word on a new line */
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, '\n');
else
Bputc(bstdout, c);
Bflush(bstdout);
prev = c;
c = Bgetc(bstdin);
}
exits(0);
}
Output
$ ./w-l
Once upon a time...
Once
upon
a
time...
tweedy