469,129 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,129 developers. It's quick & easy.

Giving the histogram a shot...

Ok I thought I would try to take the program one thing at a time. (If
you remember my last post I am trying to make a histogram with data on
the size of each word)
Anways first .. I obviously need to determine what a word actually is.
I wrote this program on my own without looking at the book or any
other resource once.

#include <stdio.h>
main()
{
int c;
int nword, nother;

nword = nother = 0;

while ((c = getchar()) != EOF)
{
if (c == ' ' '\t' '\n')
++nother;
else
++nword;
}
printf("Words = %d\nOther = %d", nword, nother);
}

I am basically just trying to tell the computer anything that is not a
blank space, a tab , or a newline is a word...
BUT everytime I run the program it adds to only the nother variable.
I can't figure out why, no matter what I type. I thought I had
written this program well and I even sketched it out on paper
beforehand , hehe.

I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.
Nov 13 '05 #1
27 2352
ext_u <ex***********@hotmail.com> wrote in message
news:32**************************@posting.google.c om...
Ok I thought I would try to take the program one thing at a time. (If
you remember my last post I am trying to make a histogram with data on
the size of each word)
Anways first .. I obviously need to determine what a word actually is.
I wrote this program on my own without looking at the book or any
other resource once.

#include <stdio.h>
main()
int main()
{
int c;
Encouragement:

Very good. Many novices make the mistake of
defining a 'char' for getchar() to store data
in. It must be 'int' as you have it, so it
can store EOF which is not guaranteed to fit
in a char.

int nword, nother;

nword = nother = 0;
Informative:

Rather than defining and then assigning after the fact,
you can give your variable initial values at definition time:

int nword = 0;
int nother = 0;

I recommend defining only one variable per line.
The reasons for this will become evident as you progress
(essentially makes the code easier to read and maintain,
and prevents possibly 'silly' mistakes, especially when
you start to work with pointers).


while ((c = getchar()) != EOF)
{
if (c == ' ' '\t' '\n')
++nother;
else
++nword;
}
printf("Words = %d\nOther = %d", nword, nother);
}

I am basically just trying to tell the computer anything that is not a
blank space, a tab , or a newline is a word...
More encouragement:

Well, I'm sure you realize that's not the ultimate goal,
but I'm glad to see you simplify things so you can get
*something* working.
BUT everytime I run the program it adds to only the nother variable.
I can't figure out why, no matter what I type. I thought I had
written this program well and I even sketched it out on paper
beforehand , hehe.
That is a very good idea, although in this case, it doesn't
help. :-( Your problem is a misunderstanding of operator
syntax.

I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.


Yes, you did make a (very common) novice mistake. Don't feel bad,
many others have done this. The problem is with your statement:

if (c == ' ' '\t' '\n')
++nother;

The comparison operator (==) takes exactly two operands.
You supplied 'c' as the 'left-hand' operand, and the
expression, ( ' ' || '\t' || '\n' ) as the 'right-hand'
operand. The 'logical or' operator (||) returns true
if either of its operands yields a nonzero (true) value.
None of ' ', '\t', or '\n' have a value of zero, so 'or-ing'
any or all of them together will always yield nonzero (true).

To express 'if c is equal to any of ' ', '\t', or '\n', you
need to express three distinct comparisons, 'or-d' together.
Write:

if (c == ' ' || c == '\t' || c == '\n')
++nother;
else
++nword;

You made a very good try. Great work.

-Mike


Nov 13 '05 #2

Mike Wahler <mk******@mkwahler.net> wrote in message
news:ds***************@newsread4.news.pas.earthlin k.net...

After looking at Kevin's reply, and seeing how mine
appears, it seems you might be using the wrong characters
to express logical 'or'. On a U.S. PC keyboard, it's the
shifted 'backslash' key. If you have some other keyboard,
I don't know which it is. Better check that out.

[snip]
Yes, you did make a (very common) novice mistake. Don't feel bad,
many others have done this. The problem is with your statement:

if (c == ' ' '\t' '\n')
++nother;
I copy/pasted the above from your post.

The comparison operator (==) takes exactly two operands.
You supplied 'c' as the 'left-hand' operand, and the
expression, ( ' ' || '\t' || '\n' ) as the 'right-hand'
operand. The 'logical or' operator (||) returns true
if either of its operands yields a nonzero (true) value.
None of ' ', '\t', or '\n' have a value of zero, so 'or-ing'
any or all of them together will always yield nonzero (true).

To express 'if c is equal to any of ' ', '\t', or '\n', you
need to express three distinct comparisons, 'or-d' together.
Write:

if (c == ' ' || c == '\t' || c == '\n')
++nother;
else
++nword;


Note how the 'or' operator appears different here.

-Mike

Nov 13 '05 #3
ext_u wrote:

Ok I thought I would try to take the program one thing at a time. (If
you remember my last post I am trying to make a histogram with data on
the size of each word)
Don't keep starting new threads about the same thing. By posting
a reply in the original thread, and snipping what isn't germane,
you don't have to remind people about it. And it also makes it
easier for them to look back, if needed.
Anways first .. I obviously need to determine what a word actually is.
I wrote this program on my own without looking at the book or any
other resource once.
Good.

#include <stdio.h>
main()
get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"
{
int c;
int nword, nother;

nword = nother = 0;

while ((c = getchar()) != EOF)
{
if (c == ' ' '\t' '\n')
This should be the logical or of three logical statements. As it
is it won't do what you want. Try:

if ((c == ' ') || (c == '\t') || (c == '\n'))

think about it, and you will see the difference. Some may say the
parentheses are redundant, but it makes the statement perfectly
clear.

BTW, better to use an indentation of 3 or 4 spaces, 8 is too
much. So don't use tabs (if you are using them).
++nother;
else
++nword;
}
printf("Words = %d\nOther = %d", nword, nother);
}

I am basically just trying to tell the computer anything that is not a
blank space, a tab , or a newline is a word...
You are saying that every char is a word, unless it is ...
BUT everytime I run the program it adds to only the nother variable.
I can't figure out why, no matter what I type. I thought I had
written this program well and I even sketched it out on paper
beforehand , hehe.

I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.


You are doing fine.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #4
ext_u wrote:
I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.


<snip>

We're interested in four things:

[1] Determining if a character is part of a word
[2] Finding the first character of each word.
[3] Finding the first character after a word
[4] Counting the characters in a word

I took a try at the problem; and wrote main first; and just
assumed that I could write a word_char() function - so all I
needed to worry about were [2], [3], and [4]. I added EOF to your
list of getchar() input values that could not appear in a word
and then added the word_char() function to satisfy [1].

#include <stdio.h>

int word_char(int c)
{ return (c != ' ') && (c != '\t') && (c != '\n') && (c != EOF);
}

int main(void)
{ int c, chars=0, count[64], i, in_word=0;

for (i=0; i<64; i++) count[i] = 0;

do
{ c = getchar();
if (!in_word && word_char(c))
{ in_word = 1;
chars = 1;
}
else if (in_word)
{ if (word_char(c)) ++chars;
else
{ ++count[chars];
in_word = 0;
}
}
} while (c != EOF);

for (i=1; i<64; i++)
{ if (count[i])
printf("There were %d words with %d letters\n",
count[i], i);
}
return 0;
}

The program makes the assumption that there won't be any words
with more than 64 characters, and may behave /very/ badly if a
longer word is encountered; but I wanted to write a simple "quick
and dirty" example. I made the assumption that you've already
encountered the do {} while () loop construction.

HTH
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #5
Mike Wahler <mk******@mkwahler.net> wrote:
[...]
Yes, you did make a (very common) novice mistake. Don't feel bad,
many others have done this. The problem is with your statement:

if (c == ' ' ?? '\t' ?? '\n')
++nother;

The comparison operator (==) takes exactly two operands.
You supplied 'c' as the 'left-hand' operand, and the
expression, ( ' ' || '\t' || '\n' ) as the 'right-hand'
operand.
At risk of over-complicating the thread, == has higher precedence than
|| (for the OP, the table on page 53 of K&R2 is worth bookmarking...),
so the operands of the == operator in this case are c and ' '. In fact,
you rely on this precedence later in your message:

[...] Write:

if (c == ' ' || c == '\t' || c == '\n')


- Kevin.

Nov 13 '05 #6
CBFalconer wrote:
ext_u wrote:
.... snip ...
#include <stdio.h>
main()


get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"


Make that last "char **argv". Someone e-mailed me but didn't
bother to put the correction up here. And no, the compiler won't
diagnose it.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #7
In article <3F***************@yahoo.com>, cb********@yahoo.com says...
CBFalconer wrote:
ext_u wrote:
... snip ...
#include <stdio.h>
main()


get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"


Make that last "char **argv". Someone e-mailed me but didn't
bother to put the correction up here. And no, the compiler won't
diagnose it.


You sure you want that semicolon up there instead of a comma? :-)

Nov 13 '05 #8
ext_u wrote:
Ok I thought I would try to take the program one thing at a time. (If
you remember my last post I am trying to make a histogram with data on
the size of each word) if (c == ' ' '\t' '\n')


This doesn't mean what you thought it meant. It means

if c == ' '
or '\t' != 0
or '\n' != 0

and, since neither the tab character nor the newline character are
equal to 0, the condition is always true. You want

if (c == ' ' || c == '\t' || c == '\n') ...
or
#include <ctype.h>

if (isspace( c )) ...

if you're prepared to accept form feed, carraige return, and vertical
tab as separators as well.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #9
Randy Howard wrote:
cb********@yahoo.com says...
CBFalconer wrote:
ext_u wrote:
>

... snip ...
>
> #include <stdio.h>
> main()

get in the habit of writing "int main(void)" or
"int main(int argc; char *argv)"


Make that last "char **argv". Someone e-mailed me but didn't
bother to put the correction up here. And no, the compiler won't
diagnose it.


You sure you want that semicolon up there instead of a comma? :-)


Woops. Yes, that one would get diagnosed. :-)

I found something from ext_u on my spam trap. If he has trouble
with DJGPP the place to go is comp.os.msdos.djgpp.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #10

"ext_u" <ex***********@hotmail.com> wrote in message

Ok I thought I would try to take the program one thing at a time.

What you want to do is break down the program into discrete functions.

eg to count the number of words, why not write a function

int countwords(char *line);

That takes a line of text as a parameter (NUL-terminated) and counts the
number of words it contains?

Nov 13 '05 #11
There is nothing wrong with the approach you've taken (scanning along a
character at a time), and others have covered the details of your
problems. You might also consider another avenue: the use of the standard
library string functions:

7.21.5.3 The strcspn function
Synopsis
1 #include <string.h>
size_t strcspn(const char *s1, const char *s2);

Description
2 The strcspn function computes the length of the maximum initial
segment of the string pointed to by s1 which consists entirely of
characters not from the string pointed to by s2.

Returns
3 The strcspn function returns the length of the segment.

By combining calls to strcspn to determine the length of a word (and
bypass it), and strspn to determine the length of white space (and bypass
it) you can very quickly step through the data in clean manner that makes
reading the code straightforward.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #12
ext_u wrote:
Ok I thought I would try to take the program one thing at a time.
Excellent!

I see that you have already had lots of technical help on this, so I'd like
to take a slightly different approach.
(If
you remember my last post I am trying to make a histogram with data on
the size of each word)
Anways first .. I obviously need to determine what a word actually is.
I wrote this program on my own without looking at the book or any
other resource once.
Don't be afraid to consult the book even when doing the exercises, unless
you are very confident that you know precisely what to do.

<snip>
I know you guys will probably find a horribly noobish mistake , but
please remember I started learning C all of like 48 hours ago.


You might be taking it a bit quickly. "The C Programming Language" is not a
"Dummies" book. It is "information-dense". That is to say, there's a huge
amount of information on every page. It might be a good idea to go a little
more slowly, and be sure that you are absorbing each new concept as it
unfolds.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #13
On Thu, 21 Aug 2003 12:30:39 GMT, CBFalconer <cb********@yahoo.com>
wrote:
If he has problems with the char '|' for any reason, or simply for
clarity, he can #include <iso646.h> and then use "or", "and" for
those confusing (to the newbie at least) tokens. His line would
then read:

if ((c == ' ') or (c == '\t') or (c == '\n'))


And the not so readable:

if ((c == ' ') ??! (c == '\t') ??! (c == '\n'))

Nick.

Nov 13 '05 #14

Nick Austin <ni**********@nildram.co.uk> wrote in message
news:0j********************************@4ax.com...
On Thu, 21 Aug 2003 12:30:39 GMT, CBFalconer <cb********@yahoo.com>
wrote:
If he has problems with the char '|' for any reason, or simply for
clarity, he can #include <iso646.h> and then use "or", "and" for
those confusing (to the newbie at least) tokens. His line would
then read:

if ((c == ' ') or (c == '\t') or (c == '\n'))


And the not so readable:

if ((c == ' ') ??! (c == '\t') ??! (c == '\n'))


if((c == ' ') + (c == '\t') + (c == '\n'))

:-)

-Mike

Nov 13 '05 #15
Mike Wahler <mk******@mkwahler.net> wrote:
Nick Austin <ni**********@nildram.co.uk> wrote in message
news:0j********************************@4ax.com...
On Thu, 21 Aug 2003 12:30:39 GMT, CBFalconer <cb********@yahoo.com>
wrote:
>If he has problems with the char '|' for any reason, or simply for
>clarity, he can #include <iso646.h> and then use "or", "and" for
>those confusing (to the newbie at least) tokens. His line would
>then read:
>
> if ((c == ' ') or (c == '\t') or (c == '\n'))
And the not so readable:

if ((c == ' ') ??! (c == '\t') ??! (c == '\n'))

if((c == ' ') + (c == '\t') + (c == '\n'))


Unfortunately, this lacks the short-circuit evaluation mechanism :)

Alex
Nov 13 '05 #16

Alex <al*******@hotmail.com> wrote in message
news:Rn***********************@news1.calgary.shaw. ca...
Mike Wahler <mk******@mkwahler.net> wrote:
Nick Austin <ni**********@nildram.co.uk> wrote in message
news:0j********************************@4ax.com...
On Thu, 21 Aug 2003 12:30:39 GMT, CBFalconer <cb********@yahoo.com>
wrote:

>If he has problems with the char '|' for any reason, or simply for
>clarity, he can #include <iso646.h> and then use "or", "and" for
>those confusing (to the newbie at least) tokens. His line would
>then read:
>
> if ((c == ' ') or (c == '\t') or (c == '\n'))

And the not so readable:

if ((c == ' ') ??! (c == '\t') ??! (c == '\n'))

if((c == ' ') + (c == '\t') + (c == '\n'))


Unfortunately, this lacks the short-circuit evaluation mechanism :)


But it still gives the correct result. :-)
User: "The machine froze up."
Me: "There's a short circuit between the chair and the keyboard"

-Mike

Nov 13 '05 #17
Malcolm wrote:

"ext_u" <ex***********@hotmail.com> wrote in message

Ok I thought I would try to take the program one thing at a time.
What you want to do is break down the program into discrete functions.


Nit #1: Functions haven't been covered at this point in the book (K&R2 p24);
they are introduced in the next section.
eg to count the number of words, why not write a function


Nit #2: Exercise 1-13 asks for a histogram of the *lengths* of the words in
the input and Exercise 1-14 asks for a histogram of the frequencies of
different *characters* in the input. Neither needs the number of words.

To the OP: There is a lot of good advice in this thread about how to approach
the problem. Another thing to keep in mind is how you are going the *test*
your program to make sure that does what you intended. Sometimes this can be
more difficult than writing the actual code.

--
Tim Hagan
Nov 13 '05 #18
Ok..
I finally got the word counting program I was working on to work. I
had a problem getting it to count each char and each word seperatly.
In the original program it was counting words and chars as the same
thing..

Here is my new try .. only took me a couple hours :)

#include <stdio.h>
main()
{
int c;
int nother, nword;
int i;

i = 0;
nword = 0;
nother = 0;

while ((c = getchar ()) != EOF){
++nother;
if (c == ' ' || c == '\t' || c == '\n')
i = 0;
else if (i == 0) {
i = 1;
++nword;
}
}
printf("words: %d chars: %d", nword, nother);
}
Nov 13 '05 #19
Mike Wahler <mk******@mkwahler.net> wrote:
Alex <al*******@hotmail.com> wrote in message
news:Rn***********************@news1.calgary.shaw. ca...
Mike Wahler <mk******@mkwahler.net> wrote:
> Nick Austin <ni**********@nildram.co.uk> wrote in message
> news:0j********************************@4ax.com...
>> On Thu, 21 Aug 2003 12:30:39 GMT, CBFalconer <cb********@yahoo.com>
>> wrote:
>>
>> >If he has problems with the char '|' for any reason, or simply for
>> >clarity, he can #include <iso646.h> and then use "or", "and" for
>> >those confusing (to the newbie at least) tokens. His line would
>> >then read:
>> >
>> > if ((c == ' ') or (c == '\t') or (c == '\n'))
>>
>> And the not so readable:
>>
>> if ((c == ' ') ??! (c == '\t') ??! (c == '\n'))
> if((c == ' ') + (c == '\t') + (c == '\n'))


Unfortunately, this lacks the short-circuit evaluation mechanism :)

But it still gives the correct result. :-)
User: "The machine froze up."
Me: "There's a short circuit between the chair and the keyboard"


Indeed. But you may potentially take a horrible performance hit. :)

Alex

Nov 13 '05 #20

On Fri, 22 Aug 2003, Tim Hagan wrote:

Malcolm wrote:
"ext_u" <ex***********@hotmail.com> wrote in message

Ok I thought I would try to take the program one thing at a time.
What you want to do is break down the program into discrete functions.


Nit #1: Functions haven't been covered at this point in the book (K&R2 p24);
they are introduced in the next section.
eg to count the number of words, why not write a function


Nit #2: Exercise 1-13 asks for a histogram of the *lengths* of the words in
the input and Exercise 1-14 asks for a histogram of the frequencies of
different *characters* in the input. Neither needs the number of words.


Anti-nit: What do you suppose the Y-axis of the histogram measures?
Suppose I have 5 words of length 1 and 3 words of length 2; how many
words do I have all together? I think writing a word-counting function
is an excellent way to start this project.
To the OP: There is a lot of good advice in this thread about how to approach
the problem. Another thing to keep in mind is how you are going the *test*
your program to make sure that does what you intended. Sometimes this can be
more difficult than writing the actual code.


Definitely!

-Arthur

Nov 13 '05 #21
"Arthur J. O'Dwyer" wrote:

On Fri, 22 Aug 2003, Tim Hagan wrote:

Malcolm wrote:
"ext_u" <ex***********@hotmail.com> wrote in message
>
> Ok I thought I would try to take the program one thing at a time.
>
What you want to do is break down the program into discrete functions.
Nit #1: Functions haven't been covered at this point in the book (K&R2 p24);
they are introduced in the next section.
eg to count the number of words, why not write a function


Nit #2: Exercise 1-13 asks for a histogram of the *lengths* of the words in
the input and Exercise 1-14 asks for a histogram of the frequencies of
different *characters* in the input. Neither needs the number of words.


Anti-nit: What do you suppose the Y-axis of the histogram measures?


Ooops. I realized this after re-examining my own solution to that exercise.
Suppose I have 5 words of length 1 and 3 words of length 2; how many
words do I have all together?
Why do you care what the total is? We only need the number of words of each
length to draw the histogram.
I think writing a word-counting function
is an excellent way to start this project.


Yes, but see Nit #1. :-)
To the OP: There is a lot of good advice in this thread about how to approach
the problem. Another thing to keep in mind is how you are going the *test*
your program to make sure that does what you intended. Sometimes this can be
more difficult than writing the actual code.


Definitely!


--
Tim Hagan
Nov 13 '05 #22

Alex <al*******@hotmail.com> wrote in message
news:jh***********************@news3.calgary.shaw. ca...
Mike Wahler <mk******@mkwahler.net> wrote:
Alex <al*******@hotmail.com> wrote in message
news:Rn***********************@news1.calgary.shaw. ca...
Mike Wahler <mk******@mkwahler.net> wrote:

> Nick Austin <ni**********@nildram.co.uk> wrote in message
> news:0j********************************@4ax.com...
>> On Thu, 21 Aug 2003 12:30:39 GMT, CBFalconer <cb********@yahoo.com>
>> wrote:
>>
>> >If he has problems with the char '|' for any reason, or simply for
>> >clarity, he can #include <iso646.h> and then use "or", "and" for
>> >those confusing (to the newbie at least) tokens. His line would
>> >then read:
>> >
>> > if ((c == ' ') or (c == '\t') or (c == '\n'))
>>
>> And the not so readable:
>>
>> if ((c == ' ') ??! (c == '\t') ??! (c == '\n'))

> if((c == ' ') + (c == '\t') + (c == '\n'))

Unfortunately, this lacks the short-circuit evaluation mechanism :)

But it still gives the correct result. :-)


User: "The machine froze up."
Me: "There's a short circuit between the chair and the keyboard"


Indeed. But you may potentially take a horrible performance hit. :)


I doubt it, but as indicated elsethread, this was
really just 'joke' code.

-Mike

Nov 13 '05 #23
Mike Wahler <mk******@mkwahler.net> wrote:
Alex <al*******@hotmail.com> wrote in message
news:jh***********************@news3.calgary.shaw. ca...
Mike Wahler <mk******@mkwahler.net> wrote:
> Alex <al*******@hotmail.com> wrote in message
> news:Rn***********************@news1.calgary.shaw. ca...
>> Mike Wahler <mk******@mkwahler.net> wrote:
>>
>> > Nick Austin <ni**********@nildram.co.uk> wrote in message
>> > news:0j********************************@4ax.com...
>> >> On Thu, 21 Aug 2003 12:30:39 GMT, CBFalconer <cb********@yahoo.com>
>> >> wrote:
>> >>
>> >> >If he has problems with the char '|' for any reason, or simply for
>> >> >clarity, he can #include <iso646.h> and then use "or", "and" for
>> >> >those confusing (to the newbie at least) tokens. His line would
>> >> >then read:
>> >> >
>> >> > if ((c == ' ') or (c == '\t') or (c == '\n'))
>> >>
>> >> And the not so readable:
>> >>
>> >> if ((c == ' ') ??! (c == '\t') ??! (c == '\n'))
>>
>> > if((c == ' ') + (c == '\t') + (c == '\n'))
>>
>> Unfortunately, this lacks the short-circuit evaluation mechanism :)
> But it still gives the correct result. :-)


> User: "The machine froze up."
> Me: "There's a short circuit between the chair and the keyboard"


Indeed. But you may potentially take a horrible performance hit. :)

I doubt it, but as indicated elsethread, this was
really just 'joke' code.


Notice the ':)'.

In fact, IIRC there was a period when C did not have logical
operators and bitwise operators were used for these things.
That is, a bitwise OR would have the same effect as your '+'.

Alex
Nov 13 '05 #24
Alex wrote:
In fact, IIRC there was a period when C did not have logical
operators and bitwise operators were used for these things.
That is, a bitwise OR would have the same effect as your '+'.


C documents I've seen, going back to 1974, all have logical operators.

--
pete
Nov 13 '05 #25

"The real OS2 guy" <os****@pc-rosenau.de> wrote in message

Why is making a variable size buffer and calli ng function on that
easier as to handle a stram of chars? What can be easier as to have a
simple stram where one char ges flagged or a flag gets tested when the
time comes?

The OP has quitely found the best solution available for the problem.

The problem is that, once you've processed the stream, its gone and it might
not be easy to reopen the file. This means the solution won't scale up.
The string solution is also more likely to be re-usable, and it is easier to
construct test cases.
Nov 13 '05 #26
On Mon, 25 Aug 2003 01:39:12 UTC, "Malcolm"
<ma*****@55bank.freeserve.co.uk> wrote:

"The real OS2 guy" <os****@pc-rosenau.de> wrote in message

Why is making a variable size buffer and calli ng function on that
easier as to handle a stram of chars? What can be easier as to have a
simple stram where one char ges flagged or a flag gets tested when the
time comes?

The OP has quitely found the best solution available for the problem.

The problem is that, once you've processed the stream, its gone and it might
not be easy to reopen the file. This means the solution won't scale up.
The string solution is also more likely to be re-usable, and it is easier to
construct test cases.

Where is the need to read a stream more than once when you've
processed anything?

If you needs the stram tiled in words, then do it, if you needs the
stream tiled in numbers, words, lines, char, you can do it without
touching a chare again after you've processed it.

Does you really think it is better to copy a number of characters from
here to there to there to there to process it only to copy it from
there to there again?

Does you like a very complex sample how to read a stram once, scan it
multiple times, check for highly different things - but having the
straem reading once?

try a look into bison or yacc. You can use them for any parsing you
may ever need - but as you have seldom really complex syntax rules it
is more often enough to write your parser youself. Breaking a line in
its kown components is easy, if you can't trust the soure (like that
what can come from a keyboard) you may have the need for more
extensive testing. But anyway you can work straight forward. Means
when you gets confused you can simply read away anything until you
gets a known sync point, such as a seaparator char (';' or '\n' or
something elsy you knows it is) and restart from phase 1 with it.

If your parsing needs to read ahead more than one char then it is not
too complex to write a wrapper around getc() and ungetc() to write
more than one char back into the stream. You needs the wrapper because
ungetc() can only write back one char.

But at least you saves the overhead of dynamic allocated and extended
input buffers. Even as often you may need dynamic sized output fields.

You can easy tokenise the stream without using strtok. You can easy
convert any kind of number to binary on the fly, you can easy separate
strings from another on the fly whereas you needs nothing than some
kinds of separators (whitre spaces, commas, single or double quotes,
colons, semicolons.....). Some of them may be only seperators in some
cases (e.g. decimal points during read of a float, '-'/'+' sing in
some positions......

You should know that C works with streams. A stream is nothing more
than a serial sequence of characters. Even as some functions in the
standard library concencates a number of chars to a buffer, the stream
itself lefts always as a pure sequence of chars.

So, always when you have to read an untrusted stream, it is always the
best to read it char by char to save any overhead.

When you doesn't hack but you're programming you'll find this idea
wounderful.

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de eComStation Reseller in Germany
eCS 1.1 german is in beta testing
Nov 13 '05 #27
Arthur J. O'Dwyer <aj*@andrew.cmu.edu> wrote:
On Sat, 23 Aug 2003, Alex wrote:
>> Mike Wahler <mk******@mkwahler.net> wrote:
>> > Alex <al*******@hotmail.com> wrote...
>> >> Mike Wahler <mk******@mkwahler.net> wrote:
>> >> <snip>
In fact, IIRC there was a period when C did not have logical
operators and bitwise operators were used for these things.
That is, a bitwise OR would have the same effect as your '+'.

Not quite. There was a time when C (or "proto-C") made no differentiation
in appearance between the BITWISE &| and the LOGICAL &|. That was long
before C89, and also long, long before my time, so this is all third-hand
information; but I understand that in those days, the compiler would keep
track somehow of whether the current context was "integral" or "boolean"
in nature, and apply the correct semantics depending on that context.


<snip example>

I was misinformed, this was decades before my time. Thanks.

Alex
Nov 13 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Oracle3001 | last post: by
1 post views Thread by bleh | last post: by
12 posts views Thread by KraftDiner | last post: by
2 posts views Thread by Daniel Nogradi | last post: by
5 posts views Thread by arnuld | last post: by
1 post views Thread by avenger3200 | last post: by
reply views Thread by Kurt Smith | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.