word count

A

agent mike

I am trying to count words in a text file. I am using the following code:

in_stream.get(c);
if(c == ' ' || c == '.' || c == ',')
word_count++;

and the word count is too low. If I include " .... || c == '\n'
the word count is too high as it counts returns of blank lines
as a word.

I am at my wits end. I'm in a beginning c++ class and this assignment
is nearly due.

HELP

me

over 'n' out
agent mike
 
R

red floyd

agent said:
I am trying to count words in a text file. I am using the following code:

in_stream.get(c);
if(c == ' ' || c == '.' || c == ',')
word_count++;

and the word count is too low. If I include " .... || c == '\n'
the word count is too high as it counts returns of blank lines
as a word.

You have the problem that a stream of delimiters (blanks, commas and periods) will also
bump the word count too high.

Hint:

Once you find a delimiter ('.', ',', ' ', '\t', '\n'), then flush all consecutive delimiters ("while" is your friend).

No code, you should be able to figure it out.
 
K

Karl Heinz Buchegger

agent said:
I am trying to count words in a text file. I am using the following code:

in_stream.get(c);
if(c == ' ' || c == '.' || c == ',')
word_count++;

and the word count is too low. If I include " .... || c == '\n'
the word count is too high as it counts returns of blank lines
as a word.

I am at my wits end. I'm in a beginning c++ class and this assignment
is nearly due.

Hint: Often it is a good idea to include additional output statements to figure
out why a program behaves the way it does. So I suggest:

in_stream.get(c);
cout << "Read '" << c << "'\n";
if(c == ' ' || c == '.' || c == ',') {
cout << "Found new word"\n";
word_count++;
}

Run your program again on a faulty input that is not to large
and use the additional output to analyse why your program doesn't
count the way you think it should.

Thats one of the simplest debug technique and believe me: you will
spend lots of time in debugging. So learning how to figure out where
your thinking was flawed is something you need to learn early.
And of course its fun: You think of a system (an algorithm) and
the machine shows you where your system breaks down.
 
G

Gianni Mariani

red said:
You have the problem that a stream of delimiters (blanks, commas and
periods) will also
bump the word count too high.

Hint:

Once you find a delimiter ('.', ',', ' ', '\t', '\n'), then flush all
consecutive delimiters ("while" is your friend).

No code, you should be able to figure it out.

Or you can create a state machine and count pertintent state changes.

here is some psuedo code.

state = start_state

while ( get( c ) )
{

target_state = is_in_word( c ) ? in_word_state : out_word_state;

if ( state == in_word_state ) {
if ( target_state == out_word_state ) {
++ word_count;
}
}

state = target_state;
}

if ( state == in_word_state )
++ word_count;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top