Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #72400 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-10-10 10:28 -0700 |
| Last post | 2015-10-18 20:44 -0700 |
| Articles | 20 on this page of 140 — 23 participants |
Back to article view | Back to comp.lang.c
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 1 of 7 [1] 2 3 4 5 6 7 Next page →
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-10-10 10:28 -0700 |
| Subject | Program to count words, issues with an if condition within a for loop |
| Message-ID | <1eb5a509-5cd5-46de-9609-98a123ed640c@googlegroups.com> |
Hello!
I would be grateful for the help on the following issue.
Before posting my question, I would like to mention that
I do understand that wordy explanations are irksome, and
I do try to escape those, though not this time. In attempt
to express my thoughts I did produce a lengthy explanation.
Thank you for your patience!
Here is the program, and below it is my question (or rather
questions).
// Count words and determine if a character is alphabetic
#include <stdio.h>
#include <stdbool.h>
bool alphabetic (const char c);
int count_words (const char string[]);
int main (void)
{
const char text1[] = "well, here goes.";
const char text2[] = "and here we go... again.";
printf ("%s - words = %i\n", text1, count_words(text1));
printf ("%s - words = %i\n", text2, count_words(text2));
return 0;
}
bool alphabetic (const char c)
{
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
return true;
else
return false;
}
// Function to cound the number of words in a string
int count_words (const char string[])
{
int i; /** used to sequence through each character
in the string **/
int word_count = 0;
bool look_for_word = true;
/** used as a flag to indicate whether
you are currently in the process of looking for the start of a
new word; at the beginning of execution you obviously are looking
for the start of a new word, so this flag is set to 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;
}
My question is regarding the following part:
for (i = 0; string[i] != '\0'; i++)
if (alphabetic (string [i]))
{
if (look_for_word)
{
word_count++;
look_for_word = false;
}
}
/** Quote from the book:
If the character is alphabetic, the look_for_word
flag is tested to determine if you are in the process of looking for
a new word. If you are, the value of word_count is incremented by 1,
and the flag is set to false, indicating that you are no longer looking
for the start of a new word.
If the character is alphabetic and the look_for_word flag is false, this
means that you are currently scanning inside a word. In such a case,
the loop is continued with the next character in the string. **/
This all is not clear to me. I have a different interpretation of
that part of code.
The main part that confuses me is how word_count works (I see it adding
adds up characters, but not words):
1) how does look_for_word technically indicate if I am, or am not, looking
for a new word? does it do so by scanning every letter of the word,
i.e. string[i], until it hits a blank space, or any other non-alphabatic character, which sets result of aplhabetic() function to false?
For example: let's take the phrase 'app new'
for (i = 0; string[i] != '\0'; i++)
if (alphabetic (string [i]))
{
if (look_for_word)
{
word_count++;
look_for_word = false;
}
}
The above part should, if I understand it correctly (well, I don't! :)),
do the following:
1) before the for loop starts the look_for_word is set to true;
2) then i = 0 is 'a' is tested, and it is alphabatic, thus the
program enters the outer if condition;
3) as soon as the character 'a' is discerned as an alphabetical one,
the inner if condition starts; I don't understand the meaning of
this condition: does (look_for_word) have no value as a condition,
and acts only as a "signal" that this flag will get a value later?
the value true has been initially assigned to this flag, and
has not been changed before this inner if condition; hence it's value
is still true when the program enters this inner if condition; confusing.
4) as the program detected the character 'a' as an alphabatical one,
word_count++ means that the word_count variable's value is not incremented
by one and has a value of 1 and look_for_word is set to false, because we
are inside the word now.
5) the program now moves to the second character i = 1 which is 'p'; the
test shows that it is the alphabatical one.
6) when the program starts working with 'p' character, the inner
if condition (look_for_word) is false, because it was set to false
in the previous round.
7) given word_count is within the for loop, it should be incremented
with each alphabatical character, but not with each word, and thus,
when the program is done working with 'p', word_cound should be equal
to 2. I know that the code works as it should, but I don't see how.
And so on, until the code reaches a blank space between app and new;
the count is terminated, and starts when the next alphabatical character
is found.
Here is the table they use for demonstration in the book. I don't see how
word_count gets these values; I added my erroneous numbers to show how I
see that part of the code.
i string[i] word_count look_for_word my word_count
0 true 0
0 'a' 1 false 1
1 'p' 1 false 2
2 'p' 1 false 3
3 ' ' 1 true nothing
4 'n' 2 false 4
5 'e' 2 false 5
6 'w' 2 false 6
7 '\0' 2 true nothing
**/
Please, forgive me for this enormous post.
Thank you!
[toc] | [next] | [standalone]
| From | Stephen Fisher <sfisher@panix.com> |
|---|---|
| Date | 2015-10-10 18:53 +0000 |
| Message-ID | <slrnn1inlb.2g7.sfisher@panix1.panix.com> |
| In reply to | #72400 |
On 2015-10-10, Alla _ <modelling.data@gmail.com> wrote:
> My question is regarding the following part:
>
> 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;
> 1) how does look_for_word technically indicate if I am, or am not,
> looking for a new word? does it do so by scanning every letter of the
> word, i.e. string[i], until it hits a blank space, or any other
> non-alphabatic character, which sets result of aplhabetic() function
> to false?
Although the for loop is scanning every letter, and the first if
statement is checking every letter of each word to see if it is
alphabetic, look_for_word is only true at the first character and then
word_count is incremented and then look_for_word is set to false. The
next time that alphabetic is false (a space), it sets look_for_word back
to true (I added this back to your snippet above for clarity).
> For example: let's take the phrase 'app new'
>
> for (i = 0; string[i] != '\0'; i++)
> if (alphabetic (string [i]))
> {
> if (look_for_word)
> {
> word_count++;
> look_for_word = false;
> }
> }
>
> The above part should, if I understand it correctly (well, I don't! :)),
> do the following:
>
> 1) before the for loop starts the look_for_word is set to true;
Yes.
> 2) then i = 0 is 'a' is tested, and it is alphabatic, thus the
> program enters the outer if condition;
Yes.
> 3) as soon as the character 'a' is discerned as an alphabetical one,
> the inner if condition starts; I don't understand the meaning of
> this condition: does (look_for_word) have no value as a condition,
> and acts only as a "signal" that this flag will get a value later?
>
> the value true has been initially assigned to this flag, and
> has not been changed before this inner if condition; hence it's value
> is still true when the program enters this inner if condition; confusing.
if(look_for_word) is shorthand for if(look_for_word == true)
FYI: The opposite wouuld be if(!look_for_word), which is shorthand for
if(look_for_word == false)
> 4) as the program detected the character 'a' as an alphabatical
> one, word_count++ means that the word_count variable's value is
> not incremented by one and has a value of 1 and look_for_word is
I think you meant to say it is incremented by one and now has a value of
1.
> set to false, because we are inside the word now.
Right.
> 5) the program now moves to the second character i = 1 which is
> 'p'; the test shows that it is the alphabatical one.
Right.
> 6) when the program starts working with 'p' character, the inner
> if condition (look_for_word) is false, because it was set to
> false in the previous round.
Right.
> 7) given word_count is within the for loop, it should be
> incremented with each alphabatical character, but not with each
> word, and thus, when the program is done working with 'p',
> word_cound should be equal to 2. I know that the code works as it
> should, but I don't see how.
See answer to question #3 above. The word_count is only incremented
when look_for_word was true. look_for_word is set to false after the
first letter of each word.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-10 22:23 +0200 |
| Message-ID | <561973d7$0$3077$426a74cc@news.free.fr> |
| In reply to | #72409 |
On 10/10/15 20:53, Stephen Fisher wrote: > if(look_for_word) is shorthand for if(look_for_word == true) > > FYI: The opposite wouuld be if(!look_for_word), which is shorthand for > if(look_for_word == false) > It's the opposite. The correct forms are : if (look_for_word) if ( ! look_for_word) And the forms below are their artificially obfuscated longer forms: if (look_for_word == true) if (look_for_word == false) Logic is not arithmetic and truth values should not be compared to true and false with == or != operators. We're with bool and normalized representation of true mapped to 1 here, so I'll skip the usual discussion of non-normalized truth values arising with int here. The very reason to remove operators is linguistic in nature: you get two verbs in your sentence with == or !=. A boolean expression represents a sentence with a verb conjugated at indicative mode, usually present tense. Comparison operators in boolean expressions are verbs at present tense: if (value == 666) // means "if value EQUALS 666" Therefore, this is correct: bool value_is_even= (value % 2 == 0); if (value_is_even) // means "if value IS even" While this is plain redundant with two verbs in the sentence: bool value_is_even= (value % 2 == 0); if (value_is_even == true) // means "if the fact value IS even IS true" ==================== Often in boolean variables, there's a unfortunate but common practice: making the indicative verb implicit. It is however still here, only hidden, and it should not change its use: bool value_even= (value % 2 == 0); if (value_even) // still the correct form if (value_even == true) // still redundant It is the case for look_for_word that the verb is implicit. The complete form is must_look_for_word or should_look_for_word. if (must_look_for_word) // "if one must look for word" if (look_for_word == true) // redundant with two verbs: // "if the fact that one must look for word is true" Just as for value_even, the removal of the indicative verb does not change the correct use of look_for_word. Therefore: if (look_for_word) // still the correct form if (look_for_word == true) // still redundant ==================== The practice of making verbs implicit should never extend to predicate functions however. Therefore, the predicate alphabetic() has it's verb missing, and in fact its subject too. It should have the prefix "is_" here, and in fact "char_is_": bool char_is_alphabetic (char character); Its correct use are, of course: if (char_is_alphabetic (c)) ... if ( ! char_is_alphabetic (c)) ... and never: if (char_is_alphabetic (c) == true) ... if (true == char_is_alphabetic (c)) ... if (char_is_alphabetic (c) != false) ... if (false != char_is_alphabetic (c)) ... nor: if (char_is_alphabetic (c) == false) ... if (char_is_alphabetic (c) != true) ... if (false == char_is_alphabetic (c)) ... if (true != char_is_alphabetic (c)) ... -- Regis
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-10 23:13 +0200 |
| Message-ID | <56197f81$0$3040$426a74cc@news.free.fr> |
| In reply to | #72415 |
Regarding the fragment:
==============================
bool alphabetic (const char c)
{
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
return true;
else
return false;
}
==============================
First, but that's not my point, I would rename it char_is_alphabetic()
for the reason I mentioned in my previous post;
I would also remove the const qualifier
that serves no purpose for the caller of the function;
and I would use unsigned char because I want a know the signedness
of chars when I do arithmetic operations such as comparisons on them.
I'll also skip the usual discussion about the existence of charsets
where lowercase and uppercase letters do not form an intervals,
and I will assume ASCII compatible charset here.
My point is: why always making useless case studies
with boolean expressions when nobody even thinks of it
with other types, say integers ?
If you score as many points as the throw of a 6-die,
you'd naturally write: score= die;
or returned from a function: return die;
and never the useless case study below:
if (die == 1) return 1;
else if (die == 2) return 2;
else if (die == 3) return 3;
else if (die == 4) return 4;
else if (die == 5) return 5;
else return 6;
So why people always write the useless case study:
if (some_fact_holds) return true;
else return false;
instead of the correct: return some_fact_holds;
A character is uppercase iff it's in the range 'A'..'Z',
therefore, it is written just as it reads above:
bool char_is_uppercase (unsigned char c) {
return 'A' <= c && c <= 'Z';
}
and not with the useless case study below:
bool char_is_uppercase (unsigned char c) {
if ('A' <= c && c <= 'Z') return true;
else return false;
}
The same holds for the similar char_is_lowercase()
bool char_is_lowercase (unsigned char c) {
return 'a' <= c && c <= 'z';
}
And the same holds of course for char_is_alphabetic(),
a character being alphabetic iff it's uppercase or lowercase:
bool char_is_alphabetic (unsigned char c) {
return char_is_lowercase (c)
|| char_is_uppercase (c);
}
or, if we leave the original nesting of || and && in place:
bool char_is_alphabetic (unsigned char c) {
return ('a' <= c && c <= 'z')
|| ('A' <= c && c <= 'Z');
}
--
regis
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-10 22:04 +0100 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <mvbue5$v4s$1@dont-email.me> |
| In reply to | #72409 |
On 10/10/2015 20:12, Stefan Ram wrote: > Maybe C books should teach C using a specific operating > system right from the start. What operating system aspects did you have in mind? > A widespread operating system > is Microsoft Windows. Maybe C should be taught showing how > to do Windows system calls right from the start? Most stuff in <windows.h> I think is too complex and too MS-centric for use in a programming course (it would probably dominate it). Maybe if there was a simple library layered onto the OS, then that same API could be implemented on a few OSes. For files, there is already C's runtime library which does that job. What's missing is, for example, a simple library for graphics (well, there are probably dozens to choose from, but none are what I'd call simple). > When explaining > via the docs which header file has to be included, it will > still be visible whether a name used belongs to the C standard > library or to the Win32 library. So learners should not confound > this. Perhaps the process of finding, downloading, installing, building and using a third party library can be a subject in itself! -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-10 14:12 -0700 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <5c2eb35a-c31d-4d56-8e14-54bc8aa25f43@googlegroups.com> |
| In reply to | #72421 |
On Saturday, October 10, 2015 at 10:05:19 PM UTC+1, Bart wrote: > > Most stuff in <windows.h> I think is too complex and too MS-centric for > use in a programming course (it would probably dominate it). > > What's missing is, for example, a simple library for graphics (well, > there are probably dozens to choose from, but none are what I'd call > simple). > You're right. Hello world is fine as a first program. But stdin - driven programs about employees aren't very motivating for most beginners. Mandelbrots are.
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-10 22:30 +0100 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <mvbvuu$4na$1@dont-email.me> |
| In reply to | #72422 |
On 10/10/2015 22:12, Malcolm McLean wrote:
> On Saturday, October 10, 2015 at 10:05:19 PM UTC+1, Bart wrote:
>>
>> Most stuff in <windows.h> I think is too complex and too MS-centric for
>> use in a programming course (it would probably dominate it).
>>
>> What's missing is, for example, a simple library for graphics (well,
>> there are probably dozens to choose from, but none are what I'd call
>> simple).
>>
> You're right.
> Hello world is fine as a first program.
> But stdin - driven programs about employees aren't very motivating
> for most beginners. Mandelbrots are.
I use the following Mandelbrot program as a benchmark. It runs in a
console screen. But I agree a high-def deep-colour version is much more
interesting.
// by Erik Wrenholt
#include <stdio.h>
#define BAILOUT 16
#define MAX_ITERATIONS 5000
int mandelbrot(double x, double y)
{
double cr = y - 0.5;
double ci = x;
double zi = 0.0;
double zr = 0.0;
int i = 0;
while(1) {
i ++;
double temp = zr * zi;
double zr2 = zr * zr;
double zi2 = zi * zi;
zr = zr2 - zi2 + cr;
zi = temp + temp + ci;
if (zi2 + zr2 > BAILOUT)
return i;
if (i > MAX_ITERATIONS)
return 0;
}
return 0;
}
int main (void) {
int x,y;
for (y = -39; y < 39; y++) {
printf("\n");
for (x = -39; x < 39; x++) {
int i = mandelbrot(x/40.0, y/40.0);
if (i==0)
printf("*");
else
printf(" ");
}
}
return 0;
}
--
bartc
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-10-10 18:25 -0400 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <mvc34u$ism$1@dont-email.me> |
| In reply to | #72421 |
On 10/10/2015 5:35 PM, Stefan Ram wrote:
> Bartc <bc@freeuk.com> writes:
>> On 10/10/2015 20:12, Stefan Ram wrote:
>>> Maybe C books should teach C using a specific operating
>>> system right from the start.
>> What operating system aspects did you have in mind?
>
> Calls to Win32.
>
>> Most stuff in <windows.h> I think is too complex
>
> I remember that once I was annoyed by Screen Savers in
> classrooms. I thought: »I need a program to easily disable
> them instead of having to go through the settings dialogs.«
>
> #include <windows.h>
>
> #define SetValue(y,x) \
> SystemParametersInfo\
> ( y, x, NULL, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE );
>
> int main( void )
> { SetValue( SPI_SETSCREENSAVEACTIVE, 0 );
> SendMessage( HWND_BROADCAST, WM_WININICHANGE, 0, 0 );
> return 0; }
>
> The above program does not look overly complex. [...]
The difficulty is that it teaches no C, or nearly none. To
understand this code one would not consult a C text, but a
Windows API reference.
If I announce that I'll teach a trigonometry class and then
start the students off computing refraction angles with Snell's
Law, I am doing them no service.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-11 15:02 +0100 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <mvdq1d$80t$1@dont-email.me> |
| In reply to | #72421 |
On 10/10/2015 22:04, Bartc wrote: > On 10/10/2015 20:12, Stefan Ram wrote: >> A widespread operating system >> is Microsoft Windows. Maybe C should be taught showing how >> to do Windows system calls right from the start? > > Most stuff in <windows.h> I think is too complex and too MS-centric for > use in a programming course (it would probably dominate it). > What's missing is, for example, a simple library for graphics (well, > there are probably dozens to choose from, but none are what I'd call > simple). I've just had a look at SDL which seemed to tick most of the boxes, and looked deceptively promising. Until I tried to actually use it. I won't post the long review I'd written, suffice to say that several hours later I've gotten nowhere. (I'd even registered with their forum, only to find that I wasn't allowed to post to ask about anything, and no way of finding out how I could.) So scrub that one. (It anyway needs part of it to be statically linked, so not quite as useful for my purposes.) (I had had my own ideas of what a *simple* cross-platform graphics library for C might look like (for basic graphics only not also audio etc like SDL). It would consist of a single .h file, common to all platforms. And one .c file per platform. For distribution, it can be .h file + .dll file, or .h file + .so file for example. I don't think it can get simpler than that in terms of compiling and linking. Wouldn't it be great if other people had the same sorts of aims? I am actually putting together a new basic graphics library now (alas not for C) and I'm resurrecting a crude library I'd used in the 80s for screen dialogs, but it was simple and it worked.) -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-11 16:39 +0100 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <mvdvoj$si7$1@dont-email.me> |
| In reply to | #72477 |
On 11/10/2015 16:19, Stefan Ram wrote: > Bartc <bc@freeuk.com> writes: >> I've just had a look at SDL which seemed to tick most of the boxes, and >> looked deceptively promising. Until I tried to actually use it. > > When I looked several years ago, there was something named > like »Qt« and something named like »WxWidgets«, but now the > students in my C++ classes tell me that today »Qt« is used > widely (no one uses »WxWidgets«). And IIRC C++17 possibly > will come with a graphics library. > > I already was thinking about using Qt in my courses, but it > is not just a library. It also seems to be a kind of > preprocessor (it wants to transform one's source code), this > lets me hesitate. Also, I'd have to care about their licensing. Some years I'd looked at using Tkinter via C. This is the one bundled with Python, and it ought to have been usable directly from C, but no one here seemed to know exactly how. It turned out that Tkinter only runs on top of TCL, and using it required incorporating the entire TCL interpreter. That involved writing main() so that it was running TCL in some sort of event loop, during which, if you were lucky, it would deign to allow you to run your actual program! Talk about the tail wagging the dog... -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Melzzzzz <mel@zzzzz.com> |
|---|---|
| Date | 2015-10-11 17:50 +0200 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <20151011175016.1be03601@maxa-pc> |
| In reply to | #72477 |
On 11 Oct 2015 15:19:20 GMT ram@zedat.fu-berlin.de (Stefan Ram) wrote: > Bartc <bc@freeuk.com> writes: > >I've just had a look at SDL which seemed to tick most of the boxes, > >and looked deceptively promising. Until I tried to actually use it. > > When I looked several years ago, there was something named > like »Qt« and something named like »WxWidgets«, but now the > students in my C++ classes tell me that today »Qt« is used > widely (no one uses »WxWidgets«). And IIRC C++17 possibly > will come with a graphics library. > > I already was thinking about using Qt in my courses, but it > is not just a library. It also seems to be a kind of > preprocessor (it wants to transform one's source code), this > lets me hesitate. Also, I'd have to care about their licensing. > Qt was written in 90es when things like event handling was not pretty to implement. Therefore they designed moc to easify that task. Actually you don't have to use it if you don't like it. All events can be handled by overriding application event function. Of course, then you don't get what is already implemented but there is choice...
[toc] | [prev] | [next] | [standalone]
| From | "Osmium" <r124c4u102@comcast.net> |
|---|---|
| Date | 2015-10-11 11:29 -0500 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <d7vh2rFcfmbU1@mid.individual.net> |
| In reply to | #72477 |
"Bartc" wrote: > I've just had a look at SDL which seemed to tick most of the boxes, and > looked deceptively promising. Until I tried to actually use it. > > I won't post the long review I'd written, suffice to say that several > hours later I've gotten nowhere. (I'd even registered with their forum, > only to find that I wasn't allowed to post to ask about anything, and no > way of finding out how I could.) > > So scrub that one. (It anyway needs part of it to be statically linked, so > not quite as useful for my purposes.) You have some really great goals! Do I understand it properly, you want graphics but not necessarily a GUI? Is it possible that you could adopt html as a medium of exchange? A C program creates an html file and then tells the resident browser to render the file? There would be no user interaction, but to get that you are probably going to encounter message loops. A second idea. A guy who dominated C book reviews wrote a book himself. It includes some graphic capabilities that he had devised. I would look at what he did and how he did it, there might be something useful there. Someone else could surely provide the guys name, he is in the UK. I have the vague feeling that it was modeled on Turbo C. If you had a few days to wander through youtube you might find some ideas you could use. I tried <graphical calculators in c> but got restless before hitting any pay dirt..
[toc] | [prev] | [next] | [standalone]
| From | "Osmium" <r124c4u102@comcast.net> |
|---|---|
| Date | 2015-10-11 11:48 -0500 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <d7vi6dFcovrU1@mid.individual.net> |
| In reply to | #72484 |
"Osmium" wrote: > Is it possible that you could adopt html as a medium of exchange? A C > program creates an html file and then tells the resident browser to render > the file? There would be no user interaction, but to get that you are > probably going to encounter message loops. I forgot to mention one of the main advantages to this approach: as a side effect, the students learn something that is useful in and of itself, html. Also, some of them probably already know a bit about it.
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-11 18:18 +0100 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <mve5hb$j75$1@dont-email.me> |
| In reply to | #72484 |
On 11/10/2015 17:29, Osmium wrote: > "Bartc" wrote: > >> I've just had a look at SDL which seemed to tick most of the boxes, >> and looked deceptively promising. Until I tried to actually use it. >> >> I won't post the long review I'd written, suffice to say that several >> hours later I've gotten nowhere. (I'd even registered with their >> forum, only to find that I wasn't allowed to post to ask about >> anything, and no way of finding out how I could.) >> >> So scrub that one. (It anyway needs part of it to be statically >> linked, so not quite as useful for my purposes.) > > You have some really great goals! Do I understand it properly, you want > graphics but not necessarily a GUI? Graphics first, ideally as something you can trivially use from C as a change from text on a console. I've done plenty of such libraries in the past when working directly with hardware. I think there have been some libraries in C, perhaps tied to a compiler (Turbo C?), but are probably now dated. A GUI library is a necessary evil if you need interaction with a graphics app. Those are very difficult to create, but they go on top of the basic graphics, and need to deal with event messages. > Is it possible that you could adopt html as a medium of exchange? A C > program creates an html file and then tells the resident browser to > render the file? There would be no user interaction, but to get that > you are probably going to encounter message loops. Yes, there are other ways too: writing to a bitmap image, which can be done in portable C, then saving to a file and getting the OS to render it. Or writing PostScript, which saves having to create the really basic routines to draw and fill lines, shapes and text. But most people will be developing on a machine that can display graphics, and they will usually want interaction, so why not make use of the OS routines to do so? In that case they need to be tamed and simplified (no one wants to worry about re-painting windows after they've been obscured for example, or grapple with 1000 different kinds of event messages). > If you had a few days to wander through youtube you might find some > ideas you could use. I tried <graphical calculators in c> but got > restless before hitting any pay dirt.. I can do my own libraries easily enough, if I need to (but now they designed for use from scripting languages). It would be useful however if there was a standardised C-level API layer to work with. (SDL might have been that, but I don't know enough about it. And I've just found this: "SDL does not ship with any built-in mechanism for drawing or filling circles." So what on earth can it do, then?! If it only provides pixel-level get/set routines, which are implemented in the OS host anyway, how can it be 1.5MB?) -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | "Osmium" <r124c4u102@comcast.net> |
|---|---|
| Date | 2015-10-11 12:46 -0500 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <d7vlkeFdlkgU1@mid.individual.net> |
| In reply to | #72493 |
"Stefan Ram" wrote: > Bartc <bc@freeuk.com> writes: >>Graphics first, ideally as something you can trivially use from C as a >>change from text on a console. > > Old teachers grew up with computers that can be programmed > as follows: > > 10 INPUT X, Y, R > 20 CIRCLE( X, Y, R ) > > Maybe their first programming lecture notes were based on > such a language. > > All of a sudden, BASIC disappears and they are asked to give > a C class. But they have little time for preparation, so they > take their BASIC lecture notes and think, »Oh, I use the same > structure, I just translate the BASIC programs into C programs.« That doesn't look like Dartmouth BASIC to me. And Dartmouth BASIC *is* BASIC. Microsoft, for example, simply loves to capture meaningful words and use them in a Humpty-Dumpty fashion. http://www.dartmouth.edu/basicfifty/commands.html
[toc] | [prev] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2015-10-11 13:57 -0400 |
| Subject | Re: Operating Systems in Teaching |
| Message-ID | <QpxSx.13282$Bh1.7132@fx08.iad> |
| In reply to | #72495 |
On Sunday October 11 2015 13:46, in comp.lang.c, "Osmium" <r124c4u102@comcast.net> wrote: > "Stefan Ram" wrote: > > >> Bartc <bc@freeuk.com> writes: >>>Graphics first, ideally as something you can trivially use from C as a >>>change from text on a console. >> >> Old teachers grew up with computers that can be programmed >> as follows: >> >> 10 INPUT X, Y, R >> 20 CIRCLE( X, Y, R ) >> >> Maybe their first programming lecture notes were based on >> such a language. >> >> All of a sudden, BASIC disappears and they are asked to give >> a C class. But they have little time for preparation, so they >> take their BASIC lecture notes and think, »Oh, I use the same >> structure, I just translate the BASIC programs into C programs.« > > That doesn't look like Dartmouth BASIC to me. And Dartmouth BASIC *is* > BASIC. > Microsoft, for example, simply loves to capture meaningful words and use > them in a Humpty-Dumpty fashion. Looks like Amiga Basic to me (written for the Amiga by Microsoft). http://www.retro-commodore.eu/files/development/amiga-development/Commodore_Amiga_Basic.pdf -- Lew Pitcher "In Skills, We Trust" PGP public key available upon request
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2015-10-11 10:21 +1300 |
| Message-ID | <d7tdr0Fj978U1@mid.individual.net> |
| In reply to | #72400 |
Alla _ wrote:
> Hello!
<snip>
>
> // Function to cound the number of words in a string
> int count_words (const char string[])
> {
> int i; /** used to sequence through each character
> in the string **/
A small point of style: you are using C99, so you can save clutter by
restricting the loop counter scope to the loop:
for (int i = 0; string[i] != '\0'; i++)
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-10 14:55 -0700 |
| Message-ID | <fe743aef-ac6c-4ff3-9a8d-69a061009c87@googlegroups.com> |
| In reply to | #72424 |
On Saturday, October 10, 2015 at 10:21:51 PM UTC+1, Ian Collins wrote: > > A small point of style: you are using C99, so you can save clutter by > restricting the loop counter scope to the loop: > > for (int i = 0; string[i] != '\0'; i++) > I find I use the final value of the index counter frequently enough to mean i doesn't have enough scope, and it's better if all the loops look the same.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-11 00:35 +0200 |
| Message-ID | <561992a8$0$3347$426a34cc@news.free.fr> |
| In reply to | #72428 |
On 10/10/15 23:55, Malcolm McLean wrote: > On Saturday, October 10, 2015 at 10:21:51 PM UTC+1, Ian Collins wrote: >> >> A small point of style: you are using C99, so you can save clutter by >> restricting the loop counter scope to the loop: >> >> for (int i = 0; string[i] != '\0'; i++) >> > I find I use the final value of the index counter frequently enough > to mean i doesn't have enough scope, and it's better if all > the loops look the same. I disagree here: All loops should definitely not look the same. You want to inform your reader when the variable is used only inside the loop, so that he does not have to care about it later, and so that he does not have to care about it before. Therefore: for (int i...) is deffo what we want in these cases. -- regis
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-10-11 00:01 +0100 |
| Message-ID | <87oag6gwfy.fsf@bsb.me.uk> |
| In reply to | #72428 |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes: > On Saturday, October 10, 2015 at 10:21:51 PM UTC+1, Ian Collins wrote: >> >> A small point of style: you are using C99, so you can save clutter by >> restricting the loop counter scope to the loop: >> >> for (int i = 0; string[i] != '\0'; i++) >> > I find I use the final value of the index counter frequently enough > to mean i doesn't have enough scope, and it's better if all > the loops look the same. How odd. That's exactly the reason I'd want to make those loops look different. -- Ben.
[toc] | [prev] | [next] | [standalone]
Page 1 of 7 [1] 2 3 4 5 6 7 Next page →
Back to top | Article view | comp.lang.c
csiph-web