473,231 Members | 1,578 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,231 software developers and data experts.

how to count words from text

dan
this is a program to count average letters per word. i am able to
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

int main ()
{

int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;

infile.open("a:wordy.txt");
if (infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}

while(infile >> ch) {
if (isalnum(ch))
letters++;
}

//average = (letters/wordcount);
cout << "There are " << letters << " letters\n";
cout << "There are " << wordcount << " words\n";
//cout << "There average is " << average << " letters per word\n";
infile.close( );
cout << "End of program.\n";
system("pause");
return EXIT_SUCCESS;
}

thanks, Dan
Jul 19 '05 #1
9 22052
da******@aol.com (dan) wrote in
news:b0**************************@posting.google.c om:
this is a program to count average letters per word. i am able to
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

int main ()
{

int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;

infile.open("a:wordy.txt");
if (infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}

while(infile >> ch) {
if (isalnum(ch))
letters++;
}

//average = (letters/wordcount);
cout << "There are " << letters << " letters\n";
cout << "There are " << wordcount << " words\n";
//cout << "There average is " << average << " letters per
word\n";
infile.close( );
cout << "End of program.\n";
system("pause");
return EXIT_SUCCESS;
}

thanks, Dan


total_words = total_number_of_spaces_and_tabs + 1

You'll have to take into account numbers and punctuation, etc, if you
need accuracy.
Jul 19 '05 #2


dan wrote:

this is a program to count average letters per word. i am able to
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.


First of all: define 'word'.
Hint: The important part is: Where does a word end?
If you see a sequence of characters, how do you recognize that
a word has ended?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3


troglobyte wrote:


total_words = total_number_of_spaces_and_tabs + 1


This is a sentence which consists of 9 words.

Now count the spaces in the above: 16

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4
Karl Heinz Buchegger <kb******@gascad.at> wrote in
news:3F***************@gascad.at:


troglobyte wrote:


total_words = total_number_of_spaces_and_tabs + 1


This is a sentence which consists of 9 words.

Now count the spaces in the above: 16


That's why I put the "etc" in there <G>
I would run the text file through a filter that removed extraneous
spaces/tabs first.

Your sentence only contains 8 words, one is a numeral.
Jul 19 '05 #5
Karl Heinz Buchegger <kb******@gascad.at> wrote in news:3F***************@gascad.at:


troglobyte wrote:


total_words = total_number_of_spaces_and_tabs + 1

That should have been -1

This is a sentence which consists of 9 words.

Now count the spaces in the above: 16

Jul 19 '05 #6

"dan" <da******@aol.com> wrote in message
news:b0**************************@posting.google.c om...
this is a program to count average letters per word. i am able to
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

int main ()
{

int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;

infile.open("a:wordy.txt");
if (infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}

while(infile >> ch) {
if (isalnum(ch))
letters++;
}

//average = (letters/wordcount);
cout << "There are " << letters << " letters\n";
cout << "There are " << wordcount << " words\n";
//cout << "There average is " << average << " letters per word\n";
infile.close( );
cout << "End of program.\n";
system("pause");
return EXIT_SUCCESS;
}

thanks, Dan


I can think of modifying your while loop like this and get to work for most
cases. For example, if there are adjacent blanks, the below loop whould use
a flag to control this situation.

Here is the loop:

while(infile >> ch)
{
if (isalnum(ch))
{
letters++;
if (flag)
flag = 0;
}
if ( flag == 0 && isspace(ch))
{
flag = 1;
wordcount ++;
}
}

Thanks
Praveen Kumar
Jul 19 '05 #7

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...


dan wrote:

this is a program to count average letters per word. i am able to
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.


First of all: define 'word'.
Hint: The important part is: Where does a word end?
If you see a sequence of characters, how do you recognize that
a word has ended?


To the OP:

This is a very important point. If two words are separated by at least one
blank that the whole thing is a walk in the park: You can just use istream
iterators to read the whole file and put it in a list. Afterwards just
output the list's size and you're done.

In case that words are not delimited by blanks but probably by a comma
(imagine for example: "this is my sentence,whatever ) then the whole thing
gets trickier. In that case you'll have to read the file line by line,
tokenize each line looking for the appropriate delimiters and count the
tokens.

HTH
Chris


Jul 19 '05 #8
"sahukar praveen" <sa************@yahoo.co.in> wrote in message news:<3f******@usenet01.boi.hp.com>...
"dan" <da******@aol.com> wrote in message
news:b0**************************@posting.google.c om...
this is a program to count average letters per word. i am able to
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

int main ()
{

int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;

infile.open("a:wordy.txt");
if(infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}

while(infile >> ch) {
if (isalnum(ch))
letters++;
}

//average = (letters/wordcount);
cout << "There are " << letters << " letters\n";
cout << "There are " << wordcount << " words\n";
//cout << "There average is " << average << " letters per word\n";
infile.close( );
cout << "End of program.\n";
system("pause");
return EXIT_SUCCESS;
}

thanks, Dan


Why read the file one character at a time. Assuming that you are defining
a word as something with at least one whitespace character of some kind on
either side of it I would do it like this. It does what you describe without
having to worry about checking what any of the characters are.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(void)
{
int wordcount = 0;
int lettercount = 0;
int average;
string word;
ifstream file;

file.open("filename");

if(!file)
{
cout << "Could not open file" << endl;
return -1;
}

while(file >> word)
{
wordcount++;
lettercount += word.length();
}

average = lettercount / wordcount;

cout << "wordcount is " << wordcount << endl;
cout << "lettercount is " << lettercount << endl;
cout << "average word length is " << average << endl;

return 0;
}
Jul 19 '05 #9

"gooch" <go******@comcast.net> wrote in message
news:d8**************************@posting.google.c om...
"sahukar praveen" <sa************@yahoo.co.in> wrote in message news:<3f******@usenet01.boi.hp.com>...
"dan" <da******@aol.com> wrote in message
news:b0**************************@posting.google.c om...
this is a program to count average letters per word. i am able to
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

int main ()
{

int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;

infile.open("a:wordy.txt");
if(infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}

while(infile >> ch) {
if (isalnum(ch))
letters++;
}

//average = (letters/wordcount);
cout << "There are " << letters << " letters\n";
cout << "There are " << wordcount << " words\n";
//cout << "There average is " << average << " letters per word\n";
infile.close( );
cout << "End of program.\n";
system("pause");
return EXIT_SUCCESS;
}

thanks, Dan


Why read the file one character at a time. Assuming that you are defining
a word as something with at least one whitespace character of some kind on
either side of it I would do it like this. It does what you describe

without having to worry about checking what any of the characters are.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(void)
{
int wordcount = 0;
int lettercount = 0;
int average;
string word;
ifstream file;

file.open("filename");

if(!file)
{
cout << "Could not open file" << endl;
return -1;
}

while(file >> word)
{
wordcount++;
lettercount += word.length();
}

average = lettercount / wordcount;

cout << "wordcount is " << wordcount << endl;
cout << "lettercount is " << lettercount << endl;
cout << "average word length is " << average << endl;

return 0;
}


Why not let the standard library do the work for us:

// create reader objects
istream_iterator<string> DataBegin( File );
istream_iterator<string> DataEnd;
list<string> DataList( DataBegin, DataEnd );

cout << DataList.size() << endl;

Regards
Chris
Jul 19 '05 #10

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

Similar topics

11
by: Foodbank | last post by:
Hello, I'm trying to develop a program that will enable me to count the number of words in a text file. As a plus, I'd like to be able to count how many different words there are too. I have a...
10
by: Tom E. | last post by:
Hello, I would like to know what the most efficient way is to count words in a string and to determine what the average word length is. I know this can be done in a array, but is there an easier...
9
by: aaron | last post by:
I have a few documents in which I need to get a total word count. Can anyone help? I'd like to create this program so I can get the result after entering the filename, and have the option to...
21
by: novel | last post by:
Is it possible to use MS Access to count the number of words in a data field?
1
by: pplers | last post by:
I got a txt file and want to count the words. That's the easy part, but then i wanted also to experiment with the different cases. For example, replace: two or more spaces with only one if they...
0
by: akshar108 via DotNetMonster.com | last post by:
Hello friends, I want to count words of pdf files through .net programming. If there is any technique then please tell me -- Message posted via DotNetMonster.com...
1
princeCpp
by: princeCpp | last post by:
-so the program input will be a string with one or more words -then the program will find the words that are four characters long and replace them all with an specified word // example input the...
4
by: jackloanshark | last post by:
Hello, I am a beginner of c. I wrote a program about counting words in a txt file.But actually there are some error in it and it will stop counting at the first paragraph and just return the number...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
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...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.