473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

word count

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
Jul 19 '05 #1
3 8797
agent mike wrote:
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.

Jul 19 '05 #2


agent mike wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3
red floyd wrote:
agent mike wrote:
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.


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;
}
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Lucas-Smith | last post by:
I am trying to use PHP's COM support to open a URL from within MS Word then save the document. I am using PHP5.0.3/Apache2/WindowsXP. phpinfo() confirms that COM support is enabled. ...
5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
8
by: scorpion53061 | last post by:
My boss is more and more asking me to get into areas of report formatting and such with the primarily database apps that I write. I am not pleased with Crystal and some of the pitfalls I have...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
1
by: beanie | last post by:
i am a c programming beginner and i am trying to Create a concordance of Word Count for a text File in c programming but my code isn't working.please can u help me out.here is my code: #include...
2
by: beanie | last post by:
i am a beginer in c programming and i am trying to Create a Concordance of Word Count for a Text File but my code is not working.pls can anyone helpme out.here is my code: #include <stdio.h>...
6
by: boyindie86 | last post by:
Hi I have been fighting with this lump of code for the last week what I am trying to do is that I am passing words into passages of texts, and I want the system to go and find exact word matches...
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.