Text word count c++

S

samoukos

Hello everyone, around one weeek ago i started to learn the c++
language. And i try to solve different programs i found from a text
book i got here in greece. But i got to a chapter and i have met a
program that wants me to count the words from an input text but not
using UTF-8 of UTF-7 but ASCII.. does anyone knows how to help me. ? I
have tryed many things but i have managed only to print out the ascii
number of each letter of a word.. nothing more....
again if anyone knows please help...

Thank you

Samuel
 
A

Alf P. Steinbach

* samoukos:
Hello everyone, around one weeek ago i started to learn the c++
language. And i try to solve different programs i found from a text
book i got here in greece. But i got to a chapter and i have met a
program that wants me to count the words from an input text but not
using UTF-8 of UTF-7 but ASCII.. does anyone knows how to help me. ? I
have tryed many things but i have managed only to print out the ascii
number of each letter of a word.. nothing more....
again if anyone knows please help...

It depends very much on the definition of "word".

If you can accept the definition that's implicit in using >>, then just
use >> (on std::cin) to input the words.

Cheers, & hth.,

- Alf
 
S

Stefano Sabatini

* samoukos:

It depends very much on the definition of "word".

If you can accept the definition that's implicit in using >>, then just
use >> (on std::cin) to input the words.

The following program implements that idea (code taken out from my
Sandbox, slightly adapted from TICPP):

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

using namespace std;

int main (int argc, char** argv) {
int num = 0;
string word;
string filename;

if (argc < 2) {
cerr << "Usage: " << argv[0] << " <filename>" << endl;
exit(1);
}
filename=argv[1];

ifstream in(filename.c_str());

while (in >> word) {
cout << word << endl;
num++;
}

cout << num << endl;
exit(0);
}

Regards.
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top