how to count words from text

D

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
 
T

troglobyte

(e-mail address removed) (dan) wrote in
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.
 
K

Karl Heinz Buchegger

dan said:
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?
 
K

Karl Heinz Buchegger

troglobyte said:
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
 
T

troglobyte

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.
 
S

sahukar praveen

dan said:
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
 
C

Chris Theis

Karl Heinz Buchegger said:
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
 
G

gooch

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;
}
 
C

Chris Theis

gooch said:
"sahukar praveen" <[email protected]> wrote in message

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C language. work with text 3
How to keep count of right answer and wrong answers in C++? 0
TF-IDF 2
First time question 1
Chatbot 0
Reading from a text file, netbeans c++? 2
Count Words 11
Lexical Analysis on C++ 1

Members online

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top