Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #72400 > unrolled thread

Program to count words, issues with an if condition within a for loop

Started byAlla _ <modelling.data@gmail.com>
First post2015-10-10 10:28 -0700
Last post2015-10-18 20:44 -0700
Articles 20 on this page of 140 — 23 participants

Back to article view | Back to comp.lang.c


Contents

  Program to count words, issues with an if condition within a for loop Alla _ <modelling.data@gmail.com> - 2015-10-10 10:28 -0700
    Re: Program to count words, issues with an if condition within a for loop Stephen Fisher <sfisher@panix.com> - 2015-10-10 18:53 +0000
      Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-10 22:23 +0200
        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-10 23:13 +0200
      Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-10 22:04 +0100
        Re: Operating Systems in Teaching Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-10 14:12 -0700
          Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-10 22:30 +0100
        Re: Operating Systems in Teaching Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-10 18:25 -0400
        Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-11 15:02 +0100
          Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-11 16:39 +0100
          Re: Operating Systems in Teaching Melzzzzz <mel@zzzzz.com> - 2015-10-11 17:50 +0200
          Re: Operating Systems in Teaching "Osmium" <r124c4u102@comcast.net> - 2015-10-11 11:29 -0500
            Re: Operating Systems in Teaching "Osmium" <r124c4u102@comcast.net> - 2015-10-11 11:48 -0500
            Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-11 18:18 +0100
              Re: Operating Systems in Teaching "Osmium" <r124c4u102@comcast.net> - 2015-10-11 12:46 -0500
                Re: Operating Systems in Teaching Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-10-11 13:57 -0400
    Re: Program to count words, issues with an if condition within a for loop Ian Collins <ian-news@hotmail.com> - 2015-10-11 10:21 +1300
      Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-10 14:55 -0700
        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 00:35 +0200
        Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 00:01 +0100
          Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-10 16:21 -0700
            Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 01:24 +0100
              Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-10 17:56 -0700
                Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 03:17 +0200
                  Re: Program to count words, issues with an if condition within a   for loop Ian Collins <ian-news@hotmail.com> - 2015-10-11 14:43 +1300
                  Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 00:19 -0700
                    Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 11:02 +0100
                      Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 03:47 -0700
                        Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 16:05 +0100
                        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 17:38 +0200
                          Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 18:49 +0200
                            Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 12:37 -0700
                              Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 21:56 +0200
                                Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 13:34 -0700
                                  Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-12 10:11 +0200
                              Re: Program to count words, issues with an if condition within a for loop Geoff <geoff@invalid.invalid> - 2015-10-11 12:59 -0700
                        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 19:15 +0200
                    Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 12:48 +0200
                      Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 03:59 -0700
                        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 13:30 +0200
                          Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 14:19 +0200
                            Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 14:48 +0200
                              Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 12:20 -0700
                        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 13:49 +0200
                          Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 13:55 +0200
            Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 02:40 +0200
        Re: Program to count words, issues with an if condition within a for loop Ian Collins <ian-news@hotmail.com> - 2015-10-11 14:40 +1300
    Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-10 22:29 +0100
      Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 00:22 +0200
        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 00:57 +0200
        Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 00:46 +0100
          Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 02:23 +0200
            Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 01:28 +0100
              Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 04:21 +0200
                Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 04:32 +0200
                Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 11:06 +0100
            Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 02:55 +0200
            Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 10:36 -0700
              Re: Program to count words, issues with an if condition within a for loop Geoff <geoff@invalid.invalid> - 2015-10-15 14:37 -0700
      Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-11 10:15 +0000
        Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-12 10:37 +0200
          Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-12 11:08 +0200
            Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-14 16:27 +0200
              Re: Program to count words, issues with an if condition within a for loop Tonton Th <tTh@nowhere.invalid> - 2015-10-14 14:28 +0000
              Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-14 16:36 +0200
                Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-14 16:40 +0200
                Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-14 16:49 +0200
        Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 14:09 -0700
          Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-16 12:49 +0200
            Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-19 14:28 -0700
              Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 02:03 +0200
                Re: Program to count words, issues with an if condition within a   for loop Ian Collins <ian-news@hotmail.com> - 2015-10-20 13:13 +1300
                Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-19 20:07 -0700
                  Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 14:30 +0200
                    Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 15:08 +0200
                Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 12:18 -0700
              Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 03:03 +0200
                Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 12:21 -0700
                  Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 15:16 +0100
                    Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-27 22:29 -0700
    Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-11 10:26 +0000
      Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-11 14:09 -0700
        Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-14 11:36 +0000
          Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-14 10:23 -0700
            Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-20 16:06 +0000
      Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-12 00:19 +0200
        Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-11 17:53 -0700
          Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-12 04:15 +0200
    Re: Program to count words, issues with an if condition within a for loop Alla _ <modelling.data@gmail.com> - 2015-10-13 08:44 -0700
      Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-14 07:27 +0200
        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-14 08:53 +0200
          Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 11:13 +0100
            Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-14 12:35 +0200
            Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 13:08 +0100
              Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-14 18:11 +0200
      Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-16 04:58 -0700
        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-16 15:07 +0200
          Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-16 23:50 +0200
            Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-17 00:06 +0200
              Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-16 15:30 -0700
          Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-19 13:45 -0700
            Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 02:40 +0200
              Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 12:47 -0700
                Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 15:36 +0100
                  Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-27 22:37 -0700
                Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 15:52 +0100
                  Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 19:10 +0100
                    Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 19:17 +0100
                    Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-27 23:05 -0700
                      Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-28 03:59 -0700
                        Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-29 14:04 +0100
                      Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-29 01:07 +0100
                        Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-30 08:31 -0700
                  Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-27 22:48 -0700
            Re: Program to count words, issues with an if condition within a for loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-20 08:55 +0000
              Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 13:52 +0200
              Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 14:04 -0700
                Re: Program to count words, issues with an if condition within a for loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-25 06:44 +0000
                  Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-25 09:09 +0000
                    Re: Program to count words, issues with an if condition within a for loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-25 10:12 +0000
                      Re: Program to count words, issues with an if condition within a for loop asetofsymbols@gmail.com - 2015-10-25 03:26 -0700
                        Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-25 06:38 -0700
                          Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-25 15:23 +0000
                          Re: Program to count words, issues with an if condition within a for loop supercat@casperkitty.com - 2015-10-25 10:54 -0700
                            Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-25 12:21 -0700
                              Re: Program to count words, issues with an if condition within a for loop supercat@casperkitty.com - 2015-10-25 14:16 -0700
                                Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-25 14:52 -0700
                            Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 00:58 +0000
                              Re: Program to count words, issues with an if condition within a for loop Bartc <bc@freeuk.com> - 2015-10-26 11:50 +0000
                          Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-11-10 15:17 +0000
                            Re: Program to count words, issues with an if condition within a for loop Les Cargill <lcargill99@comcast.com> - 2015-11-10 19:27 -0600
                              Re: Program to count words, issues with an if condition within a for loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-11 07:39 +0000
                                Re: Program to count words, issues with an if condition within a for loop Les Cargill <lcargill99@comcast.com> - 2015-11-11 08:22 -0600
                            Re: Program to count words, issues with an if condition within a for loop Robert Wessel <robertwessel2@yahoo.com> - 2015-11-11 00:37 -0600
                  Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-28 08:52 -0700
    Re: Program to count words, issues with an if condition within a for loop Barry Schwarz <schwarzb@dqel.com> - 2015-10-18 12:31 -0700
      Re: Program to count words, issues with an if condition within a for loop asetofsymbols@gmail.com - 2015-10-18 15:28 -0700
        Re: Program to count words, issues with an if condition within a for loop Ian Collins <ian-news@hotmail.com> - 2015-10-19 11:40 +1300
          Re: Program to count words, issues with an if condition within a for loop asetofsymbols@gmail.com - 2015-10-18 20:41 -0700
          Re: Program to count words, issues with an if condition within a for loop asetofsymbols@gmail.com - 2015-10-18 20:44 -0700

Page 5 of 7 — ← Prev page 1 2 3 4 [5] 6 7  Next page →


#72462

Fromraltbos@xs4all.nl (Richard Bos)
Date2015-10-11 10:26 +0000
Message-ID<561a36d9.4647546@news.xs4all.nl>
In reply to#72400
Alla _ <modelling.data@gmail.com> wrote:

> bool alphabetic (const char c)
> {
>     if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
>         return true;
>     else
>         return false;
> }

I don't think anyone else has already picked up on this, and it _is_
nitpicky given that you've probably lifted this straight from the book,
but... this function is not guaranteed to work perfectly. C does not
require all letters to be contiguous and in order.
Now there are no character sets used with C that I know of* which don't
have 'a'<'z' and 'A'<'Z', so at least this function doesn't miss any
characters that _are_ in the English alphabet**, but at least one, now
less common but still used, character set, EBCDIC _does_ have gaps
within the alphabet.
That is, in EBCDIC, 'Z'-'A' is _not_ 25, and the charset goes ABCDEFGHI
(several non-printing characters) }JKLMNOPQR (more non-printing chars)
\(figure space)STUVWXYZ . If your program is ever run on an IBM mini
(not micro) computer, it will count '}' and '\' as alphabetic. Not
likely to happen, perhaps, but...

#include <ctype.h> and calling isalpha((unsigned char)c) solves this
problem portably.

Richard

* Baudot code has this property, but is too few bits for C.
** Unless you count "and per se and"...

[toc] | [prev] | [next] | [standalone]


#72517

FromKeith Thompson <kst-u@mib.org>
Date2015-10-11 14:09 -0700
Message-ID<lnh9lxcdu7.fsf@kst-u.example.com>
In reply to#72462
raltbos@xs4all.nl (Richard Bos) writes:
> Alla _ <modelling.data@gmail.com> wrote:
>> bool alphabetic (const char c)
>> {
>>     if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
>>         return true;
>>     else
>>         return false;
>> }
>
> I don't think anyone else has already picked up on this, and it _is_
> nitpicky given that you've probably lifted this straight from the book,
> but... this function is not guaranteed to work perfectly. C does not
> require all letters to be contiguous and in order.

I think it has been mentioned, but it doesn't hurt to mention it again.

> Now there are no character sets used with C that I know of* which don't
> have 'a'<'z' and 'A'<'Z', so at least this function doesn't miss any
> characters that _are_ in the English alphabet**, but at least one, now
> less common but still used, character set, EBCDIC _does_ have gaps
> within the alphabet.
> That is, in EBCDIC, 'Z'-'A' is _not_ 25, and the charset goes ABCDEFGHI
> (several non-printing characters) }JKLMNOPQR (more non-printing chars)
> \(figure space)STUVWXYZ . If your program is ever run on an IBM mini
> (not micro) computer, it will count '}' and '\' as alphabetic. Not
> likely to happen, perhaps, but...
>
> #include <ctype.h> and calling isalpha((unsigned char)c) solves this
> problem portably.
>
> * Baudot code has this property, but is too few bits for C.
> ** Unless you count "and per se and"...

isalpha() is also sensitive to the current locale.  In the default "C"
locale it returns a true result only for the 52 uppercase and lowercase
letters, but in other locales it can also return a true result for other
characters -- which are likely to be outside the ranges 'A'..'Z' and
'a'..'z'.

So you have to ask yourself: Do you want to match just the 52 Latin
uppercase and lowercase letters, or do you want to match *all*
alphabetic characters.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#72824

Fromraltbos@xs4all.nl (Richard Bos)
Date2015-10-14 11:36 +0000
Message-ID<561e3d8e.7652703@news.xs4all.nl>
In reply to#72517
Keith Thompson <kst-u@mib.org> wrote:

> raltbos@xs4all.nl (Richard Bos) writes:

> > #include <ctype.h> and calling isalpha((unsigned char)c) solves this
> > problem portably.

> isalpha() is also sensitive to the current locale.  In the default "C"
> locale it returns a true result only for the 52 uppercase and lowercase
> letters, but in other locales it can also return a true result for other
> characters -- which are likely to be outside the ranges 'A'..'Z' and
> 'a'..'z'.
> 
> So you have to ask yourself: Do you want to match just the 52 Latin
> uppercase and lowercase letters, or do you want to match *all*
> alphabetic characters.

In theory, yes, though I don't think Alla need worry about that yet. In
fact, I think most of us don't need to worry about that, ever: the
locale starts in "C", I never change it to another myself unless that is
expressly what I want (which has never happened in serious code yet),
and if a user wants to set the locale to another value through a system
variable or some other method, well, if he's clever enough to know how
to do that he's also clever enough to know what he's getting.

Richard

[toc] | [prev] | [next] | [standalone]


#72859

FromKeith Thompson <kst-u@mib.org>
Date2015-10-14 10:23 -0700
Message-ID<lnwpup8iv7.fsf@kst-u.example.com>
In reply to#72824
raltbos@xs4all.nl (Richard Bos) writes:
> Keith Thompson <kst-u@mib.org> wrote:
>> raltbos@xs4all.nl (Richard Bos) writes:
>> > #include <ctype.h> and calling isalpha((unsigned char)c) solves this
>> > problem portably.
>
>> isalpha() is also sensitive to the current locale.  In the default "C"
>> locale it returns a true result only for the 52 uppercase and lowercase
>> letters, but in other locales it can also return a true result for other
>> characters -- which are likely to be outside the ranges 'A'..'Z' and
>> 'a'..'z'.
>> 
>> So you have to ask yourself: Do you want to match just the 52 Latin
>> uppercase and lowercase letters, or do you want to match *all*
>> alphabetic characters.
>
> In theory, yes, though I don't think Alla need worry about that yet.

Agreed -- but it's worth keeping in mind that the meaning of a "letter"
can go well beyond the 52 uppercase and lowercase letters we usually
think about.

> In fact, I think most of us don't need to worry about that, ever: the
> locale starts in "C", I never change it to another myself unless that
> is expressly what I want (which has never happened in serious code
> yet), and if a user wants to set the locale to another value through a
> system variable or some other method, well, if he's clever enough to
> know how to do that he's also clever enough to know what he's getting.

Even if the environment configures some odd locale, a C program uses the
"C" locale unless it explicitly calls setlocale() or equivalent.  On
Unix-like systems, setlocale("") sets the locale based on environment
variables; the C standard refers to this as "the locale-specific native
environment".  Without such a call, you can assume that isalpha() will
return a true value only for the 52 uppercase and lowercase letters.
But it may not be safe to assume that your code will never be used in a
program that does call setlocale().

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#73418

Fromraltbos@xs4all.nl (Richard Bos)
Date2015-10-20 16:06 +0000
Message-ID<56266675.12373375@news.xs4all.nl>
In reply to#72859
Keith Thompson <kst-u@mib.org> wrote:

> raltbos@xs4all.nl (Richard Bos) writes:
> > Keith Thompson <kst-u@mib.org> wrote:
> >> raltbos@xs4all.nl (Richard Bos) writes:
> >> > #include <ctype.h> and calling isalpha((unsigned char)c) solves this
> >> > problem portably.
> >
> >> isalpha() is also sensitive to the current locale.  In the default "C"
> >> locale it returns a true result only for the 52 uppercase and lowercase
> >> letters, but in other locales it can also return a true result for other
> >> characters -- which are likely to be outside the ranges 'A'..'Z' and
> >> 'a'..'z'.
> >> 
> >> So you have to ask yourself: Do you want to match just the 52 Latin
> >> uppercase and lowercase letters, or do you want to match *all*
> >> alphabetic characters.
> >
> > In theory, yes, though I don't think Alla need worry about that yet.
> 
> Agreed -- but it's worth keeping in mind that the meaning of a "letter"
> can go well beyond the 52 uppercase and lowercase letters we usually
> think about.

True.
Mind, I strongly suspect Alla's native alphabet has at least as many
accented letters as mine...

Richard

[toc] | [prev] | [next] | [standalone]


#72527

Fromregis <regis.barbanchon@free.fr>
Date2015-10-12 00:19 +0200
Message-ID<561ae082$0$21265$426a34cc@news.free.fr>
In reply to#72462
On 10/11/15 12:26, Richard Bos wrote:
> Alla _ <modelling.data@gmail.com> wrote:
>
>> bool alphabetic (const char c)
>> {
>>      if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
>>          return true;
>>      else
>>          return false;
>> }
>
> I don't think anyone else has already picked up on this, and it _is_
> nitpicky given that you've probably lifted this straight from the book,
> but... this function is not guaranteed to work perfectly. C does not
> require all letters to be contiguous and in order.
> Now there are no character sets used with C that I know of* which don't
> have 'a'<'z' and 'A'<'Z', so at least this function doesn't miss any
> characters that _are_ in the English alphabet**, but at least one, now
> less common but still used, character set, EBCDIC _does_ have gaps
> within the alphabet.
> That is, in EBCDIC, 'Z'-'A' is _not_ 25, and the charset goes ABCDEFGHI
> (several non-printing characters) }JKLMNOPQR (more non-printing chars)
> \(figure space)STUVWXYZ . If your program is ever run on an IBM mini
> (not micro) computer, it will count '}' and '\' as alphabetic. Not
> likely to happen, perhaps, but...
>
> #include <ctype.h> and calling isalpha((unsigned char)c) solves this
> problem portably.

Also, assuming the signedness of char is unknown,
shouldn't we care about the fact that the arithmetic may be done
here with signed char ?

In the case of, eg, EBCDIC, aphabetic ranges map to intervals as:
'a'..'i' to 129..137
'j'..'r' to 145..153
's'..'z' to 162..169
'A'..'I' to 193..201
'J'..'R' to 209..217
'S'..'Z' to 226..233

Assuming 2-complement for 8-bit signed char,
values in range 128..255 go in the negative range:

'a'= (129-256)= -127 and 'z'= (169-256)= -87
'A'= (193-256)=  -63 and 'Z'= (233-256)= -23
It's pure luck that all EBCDIC alphabetic chars are above 128
so that the lower and upper boundaries of intervals
are both remapped in negative range and hence still ordered:
-127..-87 and -63..-23

Had the tested intervals crossed the 127/128 frontier, say 100..200
so that the lower bound stay positive and upper bound becomes negative,
ie, (200-256)= -56, then the function would test for an empty interval 
100..-56.

Now assuming sign/magnitude 8-bit representation:
'a'= (128-129)=  -1 and 'z'= (128-169)= -41
'A'= (128-193)= -65 and 'Z'= (128-233)= -105
Am I right that the order of lower and upper boundaries are reversed,
and the function tests for empty intervals -1..-41 and -65..-105 ?

-- 
regis




[toc] | [prev] | [next] | [standalone]


#72541

FromKeith Thompson <kst-u@mib.org>
Date2015-10-11 17:53 -0700
Message-ID<ln8u78di0o.fsf@kst-u.example.com>
In reply to#72527
regis <regis.barbanchon@free.fr> writes:
[...]
> Also, assuming the signedness of char is unknown,
> shouldn't we care about the fact that the arithmetic may be done
> here with signed char ?

Probably not.

> In the case of, eg, EBCDIC, aphabetic ranges map to intervals as:
> 'a'..'i' to 129..137
> 'j'..'r' to 145..153
> 's'..'z' to 162..169
> 'A'..'I' to 193..201
> 'J'..'R' to 209..217
> 'S'..'Z' to 226..233

The standard requires all characters in the basic character set to be
non-negative.  That implies that if an implementation uses EBCDIC and
CHAR_BIT==8, plain char must be unsigned.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#72553

Fromregis <regis.barbanchon@free.fr>
Date2015-10-12 04:15 +0200
Message-ID<561b17b6$0$3010$426a74cc@news.free.fr>
In reply to#72541
On 10/12/15 02:53, Keith Thompson wrote:

> The standard requires all characters in the basic character set to be
> non-negative.  That implies that if an implementation uses EBCDIC and
> CHAR_BIT==8, plain char must be unsigned.
>
> [...]
>

ok, thank you ;o)

[toc] | [prev] | [next] | [standalone]


#72706

FromAlla _ <modelling.data@gmail.com>
Date2015-10-13 08:44 -0700
Message-ID<1316e081-4852-4123-bd17-f10b59cc448b@googlegroups.com>
In reply to#72400
<snip>
Thank you very much for your help! I got it now. 
Yes, I was confused by the construction of
the inner if condition statement if (look_for_word).
Thanks to this thread I've learned important tips.

Ben, I still have to look into the code you suggested
at the beginning; I don't yet understand the way it works.

int word_count = 0;
  for (int i = 0; string[i]; i++)
     word_count += alphabetic(string[i]) && !alphabetic(string[i+1]);
  return word_count; 

[toc] | [prev] | [next] | [standalone]


#72803

Fromregis <regis.barbanchon@free.fr>
Date2015-10-14 07:27 +0200
Message-ID<561de7cc$0$3027$426a74cc@news.free.fr>
In reply to#72706
On 10/13/15 17:44, Alla _ wrote:

> Ben, I still have to look into the code you suggested
> at the beginning; I don't yet understand the way it works.
>
> int word_count = 0;
> for (int i = 0; string[i]; i++)
>    word_count += alphabetic(string[i]) && !alphabetic(string[i+1]);
> return word_count;

There are as many words as here are ends of words.
Hence his solution counts the ends of words.

A word ends at index i when
the character at index i is alphabetic and
the character after index i is non-alphabetic.

That's what his condition stands for:
alphabetic(string[i]) && !alphabetic(string[i+1])

Now, his solution relies on the fact that booleans in C are integers,
so the can be used to increment word_count.

It does not hold in all languages though:
for example in java booleans are not integers,
and if you want to keep the same style, you'll have to write:
word_count += alphabetic(string[i]) && !alphabetic(string[i+1]) ? 0:1;
which also works in C.

The correctness also relies on the fact that in C,
string[] is null-terminated, that is, for len=strlen(string)
string[len] can be accessed and is the char '\0'
(which happens to be non-alphabetic),
hence alphabetic(string[i+1]) yields the expected result
even when i has reached the last character.

It does not hold for all languages though:
For example in Java or Python, you cannot access
access the character at index len in string.

So, had you an array of chars of known length len,
instead of a null-terminated string, you'd have to deal
with the special case of the last character array[len-1]
so that you don't attempt to read array[ (len-1)+1 ].

For example, by stopping the loop one step earlier,
and dealing with the last index out of the loop:

int word_count = 0;
for (int i = 0; array[i] < len-1; i++)
   word_count += alphabetic(array[i]) && !alphabetic(array[i+1]) ? 0:1;
word_count += alphabetic(array[len-1]) ? 0:1;
return word_count;

-- 
regis



[toc] | [prev] | [next] | [standalone]


#72805

Fromregis <regis.barbanchon@free.fr>
Date2015-10-14 08:53 +0200
Message-ID<561dfc04$0$3074$426a34cc@news.free.fr>
In reply to#72803
On 10/14/15 07:27, regis wrote:
> The correctness also relies on the fact that in C,
> string[] is null-terminated, that is, for len=strlen(string)
> string[len] can be accessed and is the char '\0'
> (which happens to be non-alphabetic),
> hence alphabetic(string[i+1]) yields the expected result
> even when i has reached the last character.
>
> It does not hold for all languages though:
> For example in Java or Python, you cannot access
> access the character at index len in string.
>
> So, had you an array of chars of known length len,
> instead of a null-terminated string, you'd have to deal
> with the special case of the last character array[len-1]
> so that you don't attempt to read array[ (len-1)+1 ].
>
> For example, by stopping the loop one step earlier,
> and dealing with the last index out of the loop:
>
> int word_count = 0;
> for (int i = 0; array[i] < len-1; i++)
>    word_count += alphabetic(array[i]) && !alphabetic(array[i+1]) ? 0:1;
> word_count += alphabetic(array[len-1]) ? 0:1;
> return word_count;

... which in turn fails if we allow len to be zero:

int word_count = 0;
for (int i = 0; array[i] < len-1; i++)
   word_count += alphabetic(array[i]) && !alphabetic(array[i+1]) ? 0:1;
word_count += len > 0 && alphabetic(array[len-1]) ? 0:1;
return word_count;

-- 
regis

[toc] | [prev] | [next] | [standalone]


#72812

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-10-14 11:13 +0100
Message-ID<87fv1dbvvj.fsf@bsb.me.uk>
In reply to#72805
regis <regis.barbanchon@free.fr> writes:

> On 10/14/15 07:27, regis wrote:
>> The correctness also relies on the fact that in C,
>> string[] is null-terminated, that is, for len=strlen(string)
>> string[len] can be accessed and is the char '\0'
>> (which happens to be non-alphabetic),
>> hence alphabetic(string[i+1]) yields the expected result
>> even when i has reached the last character.
>>
>> It does not hold for all languages though:
>> For example in Java or Python, you cannot access
>> access the character at index len in string.
>>
>> So, had you an array of chars of known length len,
>> instead of a null-terminated string, you'd have to deal
>> with the special case of the last character array[len-1]
>> so that you don't attempt to read array[ (len-1)+1 ].
>>
>> For example, by stopping the loop one step earlier,
>> and dealing with the last index out of the loop:
>>
>> int word_count = 0;
>> for (int i = 0; array[i] < len-1; i++)
>>    word_count += alphabetic(array[i]) && !alphabetic(array[i+1]) ? 0:1;
>> word_count += alphabetic(array[len-1]) ? 0:1;
>> return word_count;
>
> ... which in turn fails if we allow len to be zero:

There are a couple of typos:

> int word_count = 0;
> for (int i = 0; array[i] < len-1; i++)

You mean i < len - 1

>   word_count += alphabetic(array[i]) && !alphabetic(array[i+1]) ? 0:1;
> word_count += len > 0 && alphabetic(array[len-1]) ? 0:1;

and in both cases that should be 1:0 at the end.  (The same is true of
your parent post).

> return word_count;

If the length is known, my preference would be to initialise the word
count conditionally and then count the starts of words rather than their
ends.  I'd also start the loop at 1 in order to avoid the problem case
where len is unsigned (a size_t for example):

     int word_count = len > 0 && alphabetic(string[0]);
     for (int i = 1; i < len; i++)
          word_count += !alphabetic(string[i-1]) && alphabetic(string[i]);

It's worth pointing out that, in your examples, len must have a signed
integer type.  I'd probably write the loops it starting at 1 when when I
know len is signed because I prefer to write these things in such a way
that there's no need to check the type of len.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#72818

Fromregis <regis.barbanchon@free.fr>
Date2015-10-14 12:35 +0200
Message-ID<561e2fff$0$3062$426a74cc@news.free.fr>
In reply to#72812
On 10/14/15 12:13, Ben Bacarisse wrote:

> There are a couple of typos:
>
>> >int word_count = 0;
>> >for (int i = 0; array[i] < len-1; i++)
> You mean i < len - 1

yep

>> >   word_count += alphabetic(array[i]) && !alphabetic(array[i+1]) ? 0:1;
>> >word_count += len > 0 && alphabetic(array[len-1]) ? 0:1;

> and in both cases that should be 1:0 at the end.  (The same is true of
> your parent post).

yep too ;o)

thanks.

[toc] | [prev] | [next] | [standalone]


#72827

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-10-14 13:08 +0100
Message-ID<874mhtbqkt.fsf@bsb.me.uk>
In reply to#72812
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
<snip>
> [...]  I'd probably write the loops it starting at 1 when when I
> know len is signed [...]

Goodness, that's bad.  "I'd probably write the loops starting at 1 even
when I know that len is signed..."

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#72853

Fromregis <regis.barbanchon@free.fr>
Date2015-10-14 18:11 +0200
Message-ID<561e7ea5$0$3354$426a74cc@news.free.fr>
In reply to#72827
On 10/14/15 14:08, Ben Bacarisse wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> <snip>
>> [...]  I'd probably write the loops it starting at 1 when when I
>> know len is signed [...]
>
> Goodness, that's bad.  "I'd probably write the loops starting at 1 even
> when I know that len is signed..."

Regarding the initial implementation of the function
(the one in the OP message), I have the feeling that the loop
tries to do the work of 3 loops here (1 outer loop + 2 inner loops):

* an inner loop that skips word letters, a bit like strspn()
* an inner loop that skips word separators, a bit like strcspn()
* an outer loop that count skipped word

which is reflected by this state flag look_for_word
that tries to identify in which of the two inner loops he is in
at each iteration of the outer loop.

Now, as an exercice, if we want functions doing only one thing,
then the two inner loops should go to their own functions.

Hence I would go for 3 functions:

=======================================================
One for the inner loop computing the length of words.
A variation on strspn() with a predicate...
=======================================================

typedef bool Char_test (char self);

int
String_countAccepted (char const self[], Char_test * test, int start)
{
   int count= 0;
   for (int k= start;  self[k] != '\0'; k++) {
     if ( ! test (self[k])) return count;
     count++;
   }
   return count;
}

=======================================================
One for the inner loop computing the length of word separators.
A variation on strcspn() with a predicate...
=======================================================

int
String_countRejected (char const self[], Char_test * test, int start)
{
   int count= 0;
   for (int k= start;  self[k] != '\0'; k++) {
     if (test (self[k])) return count;
     count++;
   }
   return count;
}

=======================================================
One for the outer loop counting words...
=======================================================

int
String_countWords (char const self[], Char_test * isLetter)
{
   int sepLength, wordLength, wordCount= 0;
   for (int k= 0; ; k += sepLength + wordLength) {
     sepLength=  String_countRejected (self, isLetter, k);
     wordLength= String_countAccepted (self, isLetter, k+sepLength);
     if (wordLength == 0) return wordCount;
     wordCount++;
   }
}

=======================================================
The whole thing being used like this...
=======================================================

bool
Char_isAlpha (char self)
{
   return isalpha ((unsigned char) self);
}

int
main (void)
{
   char const * strings []= {
     "", "***", "one",
     "one***two***three"   , "***one***two***three",
     "one***two***three***", "***one***two***three***",
     NULL
   };

   for (int k= 0; strings[k] != NULL; k++) {
     int wordCount= String_countWords (strings[k], Char_isAlpha);
     printf ("'%s' -> %d word(s)\n", strings[k], wordCount);
   }
   return 0;
}

[toc] | [prev] | [next] | [standalone]


#73023

FromTim Rentsch <txr@alumni.caltech.edu>
Date2015-10-16 04:58 -0700
Message-ID<kfnio67hvo2.fsf@x-alumni2.alumni.caltech.edu>
In reply to#72706
Alla _ <modelling.data@gmail.com> writes:

> Ben, I still have to look into the code you suggested
> at the beginning;  I don't yet understand the way it works.
>
> int word_count = 0;
>   for (int i = 0; string[i]; i++)
>      word_count += alphabetic(string[i]) && !alphabetic(string[i+1]);
>   return word_count; 

I expect you can understand the idea underlying the following
code:

    int word_count = 0;
    for (int i = 0; string[i]; i++)
       word_count += at_start_of_word( string, i );
    return word_count; 

The code for at_start_of_word() is fairly easy but slightly
clumsy:

    int
    at_start_of_word( const char *string, int k ){
        if(  ! alphabetic( string[k] )  )  return  0;
        if(  k == 0  )  return  1;
        if(  alphabetic( string[k-1] )  )  return  0;
        return  1;
    }

Although this function body is somewhat messy, if you work
through the various cases I think you'll be able to see why this
works.  There are other ways of getting the same result, but in
all cases there is the complication that what happens when k is
zero needs to be different from when k is greater than zero.
This occurs because 'string' doesn't have a character before
string[0].

The clever insight in Ben's suggestion is that every word that
has a beginning also has an end, and testing for the end is a lot
easier than testing for the beginning.  So the high level routine
becomes this:

    int word_count = 0;
    for (int i = 0; string[i]; i++)
       word_count += at_end_of_word( string, i );
    return word_count; 

Then, the routine at_end_of_word() has a simpler test, namely, if
the character being looked at is alphabetic, and the next
character (which may be the null character at the end of the
string) is not alphabetic, then we are at the end of a word, and
otherwise we aren't.  This allows writing a less tortuous
function body:

    int
    at_end_of_word( const char *string, int k ){
        return  alphabetic( string[k] )  &&  ! alphabetic( string[k+1] );
    }

So hopefully that somewhat detailed progression not only explains
why it works, but also gives you a way of constructing a line of
reasoning so that you can figure out other coding conundrums in
the future.

[toc] | [prev] | [next] | [standalone]


#73027

Fromregis <regis.barbanchon@free.fr>
Date2015-10-16 15:07 +0200
Message-ID<5620f6a8$0$3187$426a74cc@news.free.fr>
In reply to#73023
On 10/16/15 13:58, Tim Rentsch wrote:
> The code for at_start_of_word() is fairly easy but slightly
> clumsy:
>
>      int
>      at_start_of_word( const char *string, int k ){
>          if(  ! alphabetic( string[k] )  )  return  0;
>          if(  k == 0  )  return  1;
>          if(  alphabetic( string[k-1] )  )  return  0;
>          return  1;
>      }
>
> Although this function body is somewhat messy

Guard clauses are not messy.
Refactoring to guard clause is in fact a common refactoring
to remove nesting of some conditionals.

However, there was a solution here without any conditional nesting
that did not require the use of guard clauses:

bool
string_index_is_at_start_of_word (char const string[], int k)
{
    return (k == 0) ?
       char_is_alpha (string [k]) :
       char_is_alpha (string [k]) && ! char_is_alpha (string [k-1]));
}

That is, a char starts a word iff:
*it's alphabetic, at the beginning of the string.
*it's alphabetic and its predecessor is not, anywhere else.

[toc] | [prev] | [next] | [standalone]


#73094

Fromregis <regis.barbanchon@free.fr>
Date2015-10-16 23:50 +0200
Message-ID<56217115$0$3348$426a74cc@news.free.fr>
In reply to#73027
On 10/16/15 19:29, Stefan Ram wrote:
> regis <regis.barbanchon@free.fr> writes:
>>>       int
>>>       at_start_of_word( const char *string, int k ){
>>>           if(  ! alphabetic( string[k] )  )  return  0;
>>>           if(  k == 0  )  return  1;
>>>           if(  alphabetic( string[k-1] )  )  return  0;
>>>           return  1;
>>>       }
>>> Although this function body is somewhat messy
>> Guard clauses are not messy.
>> Refactoring to guard clause is in fact a common refactoring
>
> return !isalpha( s[ k ])? 0 : !k || !isalpha( s[ k -1 ]);
>

isalpha() expects an unsigned char or EOF encoded in its int arg.

[toc] | [prev] | [next] | [standalone]


#73096

Fromregis <regis.barbanchon@free.fr>
Date2015-10-17 00:06 +0200
Message-ID<56217500$0$4543$426a74cc@news.free.fr>
In reply to#73094
On 10/16/15 23:50, regis wrote:
> On 10/16/15 19:29, Stefan Ram wrote:
>> regis <regis.barbanchon@free.fr> writes:
>>>>       int
>>>>       at_start_of_word( const char *string, int k ){
>>>>           if(  ! alphabetic( string[k] )  )  return  0;
>>>>           if(  k == 0  )  return  1;
>>>>           if(  alphabetic( string[k-1] )  )  return  0;
>>>>           return  1;
>>>>       }
>>>> Although this function body is somewhat messy
>>> Guard clauses are not messy.
>>> Refactoring to guard clause is in fact a common refactoring
>>
>> return !isalpha( s[ k ])? 0 : !k || !isalpha( s[ k -1 ]);
>>
>
> isalpha() expects an unsigned char or EOF encoded in its int arg.

Otherwise, isalpha will fail to identify latin1 code point 0xFF
(the character ÿ, y-umlaut) as an alphabetic character
when char is signed and setlocale() sets LC_ALL to "fr_FR.iso88591":

It will mistakenly match it to EOF= -1.

(IMHO, It's a design error to have allowed EOF be a valid argument
for these predicates. There's no good reason to pass EOF
to them, and these functions should not even be aware
of the notion of files.)



[toc] | [prev] | [next] | [standalone]


#73103

FromKeith Thompson <kst-u@mib.org>
Date2015-10-16 15:30 -0700
Message-ID<lny4f230qm.fsf@kst-u.example.com>
In reply to#73096
regis <regis.barbanchon@free.fr> writes:
> On 10/16/15 23:50, regis wrote:
>> On 10/16/15 19:29, Stefan Ram wrote:
>>> regis <regis.barbanchon@free.fr> writes:
>>>>>       int
>>>>>       at_start_of_word( const char *string, int k ){
>>>>>           if(  ! alphabetic( string[k] )  )  return  0;
>>>>>           if(  k == 0  )  return  1;
>>>>>           if(  alphabetic( string[k-1] )  )  return  0;
>>>>>           return  1;
>>>>>       }
>>>>> Although this function body is somewhat messy
>>>> Guard clauses are not messy.
>>>> Refactoring to guard clause is in fact a common refactoring
>>>
>>> return !isalpha( s[ k ])? 0 : !k || !isalpha( s[ k -1 ]);
>>>
>>
>> isalpha() expects an unsigned char or EOF encoded in its int arg.
>
> Otherwise, isalpha will fail to identify latin1 code point 0xFF
> (the character ÿ, y-umlaut) as an alphabetic character
> when char is signed and setlocale() sets LC_ALL to "fr_FR.iso88591":
>
> It will mistakenly match it to EOF= -1.

That assumes that plain char is signed, and that EOF is equal to -1.
Both are commonly true, but neither is guaranteed.

Worse, given `char c = 0xfe;`, isalpha(c) has undefined behavior if char
is signed and CHAR_BIT==8.

> (IMHO, It's a design error to have allowed EOF be a valid argument
> for these predicates. There's no good reason to pass EOF
> to them, and these functions should not even be aware
> of the notion of files.)

I think the idea was that isalpha() accepts any value returned by
getchar().  But I agree that that's not particularly useful.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


Page 5 of 7 — ← Prev page 1 2 3 4 [5] 6 7  Next page →

Back to top | Article view | comp.lang.c


csiph-web