From mboxrd@z Thu Jan 1 00:00:00 1970 To: 9fans@9fans.net Date: Wed, 11 Nov 2009 12:32:41 +0000 From: frankg Message-ID: <33e7e0a4-5181-4f3f-a830-65a9d553f92e@g22g2000prf.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable References: Subject: Re: [9fans] sed question (OT) Topicbox-Message-UUID: 9967b9d0-ead5-11e9-9d60-3106f5b1d025 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 > #include > #include > > #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.