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


Groups > comp.lang.c > #72871

Re: While loop with bool, same issue

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: While loop with bool, same issue
Date Wed, 14 Oct 2015 11:42:01 -0700
Organization None to speak of
Lines 99
Message-ID <lnfv1d8f7q.fsf@kst-u.example.com> (permalink)
References <96769962-2192-444e-a738-01306756e949@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="31793"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vXsDMRQ674uMxWRW/4cHy"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock sha1:qQ8//ZjnouL4f7btBhYy3Nin4RA= sha1:v8AwNbmYdABxCjkr1AObyLsKl6o=
Xref csiph.com comp.lang.c:72871

Show key headers only | View raw


Alla _ <modelling.data@gmail.com> writes:
> I have decided to create a new topic, although
> with the issue that I have already raised previously 
> (previous one is clear to me now).
>
> /** Counting words in a piece of text **/
>
> #include <stdio.h>
> #include <stdbool.h>
>
> bool alphabetic (const char c)
> {
>     if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
>         return true;
>     else
>         return false;
> }

This is more simply written as:

    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');

Strictly speaking the parentheses aren't necessary, but they do make it
clearer.

There's not much point in defining the parameter as "const".  It has no
meaning to the caller.

> void read_line (char buffer [])
> {
>     char character;
>     int i = 0;
>     
>     do
>     {
>         character = getchar();

getchar() returns an int, but you're assigning it to a char.  That means
you can't reliably detect EOF ...

>         buffer[i] = character;
>         i++;
>     }
>     while (character != '\n');

... which you're not trying to do anyway.  It *is* possible for
getchar() to return EOF in the middle of a line.  If it does, you have
an infinite loop.

>     buffer[i - 1] = '\0'; // i - 1 because the index character was incremented one extra time inside the loop the last time it was executed
> }
> int count_words (const char string[])
> {
>     int i; 
>     int word_count = 0;
>     bool look_for_word = true;

I suggest that "looking_for_word" would be a clearer name.

>     for (i = 0; string[i] != '\0'; i++)
>         if (alphabetic (string [i]))
>         {
>             if (look_for_word)
>             {
>                 word_count++;
>                 look_for_word = false;
>             }
>         }
>     
>         else
>             look_for_word = true;
>     
>     return word_count;
> }
>
> int main (void)
> {
>     char text[81];
>     int total_words = 0;
>     bool end_of_text = false;
>     
>     printf ("Type in your text.\n");
>     printf ("When done, press 'RETURN'.\n\n");
>     
>     while ( !end_of_text ) 
>     {
>         read_line (text);

`text` is just 81 characters long.  Your read_line function will read as
many characters as you give it.  If your cat stands on the space bar,
you'll have undefined behavior.

[snip]

-- 
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"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

While loop with bool, same issue Alla _ <modelling.data@gmail.com> - 2015-10-14 08:59 -0700
  Re: While loop with bool, same issue Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-14 13:31 -0400
    Re: While loop with bool, same issue August Karlstrom <fusionfile@gmail.com> - 2015-10-14 20:11 +0200
      Re: While loop with bool, same issue Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-14 14:26 -0400
    Re: While loop with bool, same issue Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 20:44 +0100
  Re: While loop with bool, same issue Siri Cruz <chine.bleu@yahoo.com> - 2015-10-14 10:45 -0700
    Re: While loop with bool, same issue Keith Thompson <kst-u@mib.org> - 2015-10-14 13:15 -0700
  Re: While loop with bool, same issue Keith Thompson <kst-u@mib.org> - 2015-10-14 11:42 -0700
    Re: While loop with bool, same issue Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-15 08:20 +0000

csiph-web