9p / who / tweedy / 9C / 1


Exercise 1.8: Count Blanks, Tabs, and Newlines

Code

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

#define STDIN   0

/* count blanks, tabs, and new lines */

void
main()
{
    Biobuf *bstdin;
    bstdin = Bfdopen(STDIN, OREAD); 
    int c, b, t, nl;

    b = t = nl = 0;
    while((c = Bgetc(bstdin)) >= 0){
        if(c == ' ')
            ++b;
        if(c == '\t')
            ++t;
        if(c == '\n')
            ++nl;
    }
    print("%ud\t%ud\t%ud \n", b, t, nl);

    exits(0);
}

Output

$ 9c blanks.c; 9l blanks.o -o blanks
$ ./blanks < blanks.c
40  25  28 



tweedy