can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the occurance of each word. - #include <fstream>
-
#include <iostream>
-
#include <string>
-
#include <cstdlib>
-
-
using namespace std;
-
-
-
-
string getInputFileName(); // a function to prompt for the complete file name
-
-
int numCharsInFile( ifstream &in, int &numLines ); // a function to count the
-
// number of characters and
-
// lines in a text file
-
-
int numWordsInFile( ifstream &in, int &numWords ); // a function to count words in file
-
-
-
-
main ()
-
{
-
-
char c;
-
int nLines, // number of lines in the text file
-
nChars, // number of characters in the text file
-
avgCharsPerLine, // average number of characters per line
-
nWords; // number of words in the text file
-
-
-
ifstream inFile; // handle for the input text file
-
-
string fileName; // complete file name including the path
-
-
fileName = getInputFileName(); // prompt and obtain the full file name
-
-
inFile.open(fileName.c_str()); // try to open the file
-
-
if( !inFile.is_open() ) // test for unsuccessfull file opening
-
{
-
cerr << "Cannot open file: " << fileName << endl << endl;
-
exit (0);
-
}
-
-
-
nChars = numCharsInFile( inFile, nLines ); // determine the number of lines
-
// and characters in the file
-
nWords = numWordsInFile( inFile, nWords); // determine the number of words
-
-
avgCharsPerLine = nChars / nLines;
-
-
-
cout << "The number of characters in the file: " << fileName
-
<< " is = " << nChars << endl << endl;
-
-
cout << "The number of lines in the file: " << fileName
-
<< " is = " << nLines << endl << endl;
-
-
-
cout << "The number of Words in the file: " << fileName
-
<< " is = " << nWords << endl << endl;
-
-
cout << "The average number of characters per line in the text file: "
-
<< fileName << " is: " << avgCharsPerLine << endl << endl;
-
cin>>c;
-
inFile.close(); // close the input file
-
-
}
-
-
-
-
string getInputFileName()
-
{
-
string fName; // fully qualified name of the file
-
-
cout << "Please enter the fully qualified name of the " << endl
-
<< "input text file (i.e. including the path): ";
-
cin >> fName; // cannot handle blanks in a file name or path
-
cout << endl;
-
-
return fName;
-
}
-
-
-
-
-
-
int numCharsInFile( ifstream &in, int &numLines )
-
{
-
int numChars = 0;
-
-
char ch; // character holder;
-
-
numLines = 0; // initialize the number of lines to zero
-
-
while ( in.get(ch) ) // get the next character from the file
-
// the function get will also get whitespace
-
// i.e. blanks, tabs and end of line characters
-
{
-
if (ch != ' ' )
-
{
-
if(ch != '\n')
-
numChars++;// increase the count of characters by one if ch is NOT '\n' AND NOT a blank space
-
else
-
{
-
numLines++; // increase the count of lines by one if ch IS '\n'
-
}
-
}
-
}
-
numLines += 1; // for some reason it needs to add one and the results are correct
-
return numChars;
-
}
-
-
-
-
-
int numWordsInFile( ifstream &in, int &nWords)
-
{
-
in.clear();
-
-
in.seekg(0, ios_base::beg);
-
-
int numWords = 0 ;
-
-
char ch;
-
-
-
while (in.get(ch))
-
{
-
-
if ( ch == ' ' || ch == '\n' || ch == '\t' )
-
numWords++;
-
-
-
}
-
-
return numWords+1;
-
}
3 2678
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the occurance of each word.
I'm not going to write the code, but think about it: If you're going to count the occurrence of each distinct word, you'll need to remember those words. So as you read words in, you'll need to store them so that subsequent words can be compared to them. How you store them is up to you, as is how you compare them. STL containers can be a big help there. Take a look at std::set if you're not familiar with it; it's a container which can't hold duplicate elements, which lets you easily determine if a word occurs more than once in the file. Another good way to do this might be to just store each word as it is written, then sort the words in alphabetical order: recurring words will appear next to each other, making it easy to count them.
std::map would probably be better than std::set, since that way you can map strings (words) to integers (frequency counts).
Also keep in mind that the >> operator stops on whitespace. You can fetch one word by: -
sting str;
-
fileName >> str;
-
Also, you are not required to declare your variables at the beginning of each function. It looks like you have a C background and are just starting out on C++.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: The Roys |
last post by:
Hi
Im doing something wrong in quitting the Word.Application in my VB program.
I have
General Declarations
Dim AppWord As Word.Application
Form_Load()
Set AppWord =...
|
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...
|
by: agent mike |
last post by:
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 "...
|
by: STeve |
last post by:
Hey guys,
I currently have a 100 page word document filled with various
"articles". These articles are delimited by the Style of the text
(IE. Heading 1 for the various titles) These articles...
|
by: Mikey |
last post by:
Sample VB .NET source code to create mailing labels or customized letters
using MS Word MailMerge
This VB .NET source code will start MS Word and call methods and set
properties in MS Word to...
|
by: Adam Faulkner via DotNetMonster.com |
last post by:
I had a problem before extracting pages from an existing word document and
then inserting the content into a new word document.
The following code below works with Microsoft Word 2000
Function...
|
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:...
|
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...
|
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>...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |