9p / who / tweedy / 9C / 1


Count Digits, White Space, Others

Code

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

#define STDIN   0

/* count digits, white space, others */

void
main()
{
    Biobuf *bstdin;
    bstdin = Bfdopen(STDIN, OREAD);
    int c, i, nwhite, nother;
    int ndigit[10];

    nwhite = nother = 0;
    for(i = 0; i < 10; i++)
        ndigit[i] = 0;

    while((c = Bgetc(bstdin)) >= 0)
        if(c >= '0' && c <= '9')
            ++ndigit[c-'0'];
        else if(c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;

    print("digits =");
    for(i = 0; i < 10; ++i)
        print(" %ud", ndigit[i]);
    print(", white space = %ud, other = %ud\n", nwhite, nother);

    exits(0);
}

Output

$ 9c digits.c; 9l digits.o -o digits
$ ./digits < digits.c
digits = 12 3 0 0 0 0 0 0 0 1, white space = 137, other = 439



tweedy