On Oct 30, 12:58Â pm, noah.ev...@gmail.com (Noah Evans) wrote: > This kind of problem is character processing, which I would argue is > C's domain. You can massage awk and sed to do the job for you, but at > least for me it's conceptually simpler to just bang out the following > C program: > > #include <u.h> > #include <libc.h> > #include <bio.h> > > #define isupper(r) Â Â Â (L'A' <= (r) && (r) <= L'Z') > #define islower(r) Â Â Â (L'a' <= (r) && (r) <= L'z') > #define isalpha(r) Â Â Â (isupper(r) || islower(r)) > #define isspace(r) Â Â Â ((r) == L' ' || (r) == L'\t' \ > Â Â Â Â Â Â Â Â Â Â Â Â || (0x0A <= (r) && (r) <= 0x0D)) > #define toupper(r) Â Â Â ((r)-'a'+'A') > > void > usage(char *me) > { > Â Â Â Â fprint(2, "%s: usage\n", me); > > } > > void > main(int argc, char **argv) > { > Â Â Â Â Biobuf in, out; > Â Â Â Â int c, waswhite, nwords; > > Â Â Â Â ARGBEGIN{ > Â Â Â Â default: > Â Â Â Â Â Â Â Â usage(argv[0]); > Â Â Â Â }ARGEND; > Â Â Â Â Binit(&in, 0, OREAD); > Â Â Â Â Binit(&out, 1, OWRITE); > > Â Â Â Â waswhite = 0; > Â Â Â Â nwords = 0; > Â Â Â Â while((c = Bgetc(&in)) != Beof){ > Â Â Â Â Â Â Â Â if(isalpha(c)) > Â Â Â Â Â Â Â Â if(waswhite) > Â Â Â Â Â Â Â Â if(nwords < 2){ > Â Â Â Â Â Â Â Â Â Â Â Â if(islower(c)) > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â c = toupper(c); > Â Â Â Â Â Â Â Â Â Â Â Â nwords++; > Â Â Â Â Â Â Â Â } > Â Â Â Â Â Â Â Â if(isspace(c)) > Â Â Â Â Â Â Â Â Â Â Â Â waswhite = 1; > Â Â Â Â Â Â Â Â else > Â Â Â Â Â Â Â Â Â Â Â Â waswhite = 0; > Â Â Â Â Â Â Â Â if(c == '\n') > Â Â Â Â Â Â Â Â Â Â Â Â nwords = 0; > Â Â Â Â Â Â Â Â Bputc(&out, c); > Â Â Â Â } > Â Â Â Â exits(0); > > } > > Noah >
Simple, and wrong. You need to initialize waswhite to 1, not 0.