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.

Reply via email to