By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

how to count words from text

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a


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

P: n/a


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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a
"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.