473,770 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8818
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
4529
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. Manually, this would be: - Start MS Word (am using Office 2003)
5
7652
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 directory) and count number of times any given word appears in the text. #!/usr/bin/python
8
2437
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 seen with this particualar report tool. I would like to delve into Word automation ( I believe you can control page breaks and such with this). Can someone point me to a good how to with the various methods available there?
1
4216
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: (there is a WORD in ( my string WORD )) and * WORD * to (find WORD) and * WORD * Should give me the to word between star (star ar not part of string)
4
12441
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
1
3013
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 <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> struct word { struct word *left; /* tree to the left */ struct word *right; /* tree to the right */ char *WORD;
2
4965
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> #include <ctype.h> #include <string.h> #include <stdlib.h> struct word { struct word *left; /* tree to the left */ struct word *right; /* tree to the right */ char *WORD;
6
3391
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 only and place square brackets around them e.g word = car PASSAGE: the red car required a lot of care to prevent a scar I only want it to place a square bracket around the word car and ignore the car in "Care" and "scar"
0
1926
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 PreviousToken(Previous word) and frequency and probability From text file and put each one in cell in table For example if the text file content is "Every man has a price. Every woman has a price." First Token(word) is "Every" PreviousToken(Previous...
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.