473,413 Members | 2,043 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,413 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 22076
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...
0
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...

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.