Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: While loop with bool, same issue |
| Date | 2015-10-14 13:31 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <mvm3e9$fjs$1@dont-email.me> (permalink) |
| References | <96769962-2192-444e-a738-01306756e949@googlegroups.com> |
On 10/14/2015 11:59 AM, Alla _ wrote:
> Hello!
>
> 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;
> }
Aside: The `if' is unnecessary. You could simply do
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
instead. (I mention this mostly because you seem to use this
silly pattern frequently. It's not "wrong," just silly.)
> void read_line (char buffer [])
> {
> char character;
> int i = 0;
>
> do
> {
> character = getchar();
> buffer[i] = character;
> i++;
> }
> while (character != '\n');
>
> buffer[i - 1] = '\0'; // i - 1 because the index character was incremented one extra time inside the loop the last time it was executed
> }
Aside: This is, more or less, a re-implementation of the
COMPLETELY UNSAFE gets() function, a function SO BAD that it has
been removed from the latest C Standard. (I say "more or less"
because there are errors: See question 12.1 on the comp.lang.c
FAQ page at <http://www.c-faq.com/> for one of them, and you'll
learn about the other if you ever type your system's end-of-input
keystroke -- maybe CTRL-D or CTRL-Z -- at the keyboard.)
> int count_words (const char string[])
> {
> int i;
> int word_count = 0;
> bool look_for_word = true;
>
> 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);
>
> if (text[0] == '\0')
> end_of_text = true;
> /** tests the input line that is stored
> inside the text array to see if just the Enter (or Return)
> key was pressed; if so, then the buffer contains the null
> string, in which case the flag end_of_text is set to true
> to signal that all of the text has been entered **/
> else
> total_words += count_words (text);
> }
>
> printf ("\nThere are %i words in the above text.\n", total_words);
>
> return 0;
>
> }
>
> The part that I am not sure I understand correctly:
>
> while ( !end_of_text )
> {
> read_line (text);
>
> if (text[0] == '\0')
> end_of_text = true;
>
> /** tests the input line that is stored
> inside the text array to see if just the Enter (or Return)
> key was pressed; if so, then the buffer contains the null
> string, in which case the flag end_of_text is set to true
> to signal that all of the text has been entered **/
>
> else
> total_words += count_words (text);
> }
>
> The program has assigned a false value to end_of_text.
To start with, yes. (Actually, it's an initialization, not an
assignment, but that's a nitpick.)
> Hence, the condition in the while loop should mean
> end_of_text is true.
Did you miss the leading `!' character? Have you learned
yet what the `!' means?
> This is how I read each line of the code:
>
> Before the user has typed in his string, end_of_text
> is true (no text exists); therefore, while (end_of_text is true),
> get the text from the user (read_line(text).
No; see the remark about `!' above.
> If the user has only pressed Enter, set end_of_ text to true.
> Isn't end_of_text already true because we are inside the loop with
> that exact condition; and also because read_line(text) ends
> with the NULL character, thus indicating the end of the text?
No.
> Otherwise, set end_of_text to false and compute total_words.
The variable is initialized to false, but is never set false
thereafter. The only value assigned to it after initialization
is true.
> I am not sure I understand why while loop is required.
How many lines of input could you process if there were no loop?
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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