From: frankg <frankg.fgleason@gmail.com>
To: 9fans@9fans.net
Subject: Re: [9fans] sed question (OT)
Date: Wed, 11 Nov 2009 12:32:41 +0000 [thread overview]
Message-ID: <33e7e0a4-5181-4f3f-a830-65a9d553f92e@g22g2000prf.googlegroups.com> (raw)
In-Reply-To: <f631016df731e553421e6079dd1da0d4@quintile.net>
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.
next prev parent reply other threads:[~2009-11-11 12:32 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-29 15:41 Steve Simon
2009-10-29 16:06 ` Lorenzo Bolla
2009-10-29 16:33 ` Iruata Souza
2009-10-29 16:42 ` Lorenzo Bolla
2009-10-29 16:09 ` W B Hacker
2009-10-29 18:52 ` Jason Catena
2009-10-30 13:35 ` Eris Discordia
2009-10-30 13:39 ` Eris Discordia
2009-10-30 17:30 ` W B Hacker
2009-10-30 17:39 ` [9fans] sed question (OT) (OT) (OT) Tim Newsham
2009-10-30 18:14 ` [9fans] sed question (OT) (OT) (OT) (OT) (OT)(OT)(OT)(OT)(OT)(OT)(OT)(OT)(OT)(OT) W B Hacker
2009-10-30 15:29 ` [9fans] sed question (OT) dave.l
2009-10-30 20:53 ` Noah Evans
2009-11-11 12:32 ` frankg [this message]
[not found] <<80c99e790910290906t36766978kcd38c9583392e038@mail.gmail.com>
2009-10-29 16:08 ` erik quanstrom
2009-10-29 16:29 ` Iruata Souza
[not found] <<d1c554290910290929p3980a256hf075042ca3a3917b@mail.gmail.com>
2009-10-29 16:31 ` erik quanstrom
[not found] <<A3AADD7F-E09D-49F9-8A5B-3D6B720046A4@mac.com>
2009-10-30 16:16 ` erik quanstrom
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=33e7e0a4-5181-4f3f-a830-65a9d553f92e@g22g2000prf.googlegroups.com \
--to=frankg.fgleason@gmail.com \
--cc=9fans@9fans.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).