| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to count words?
The code is this
Code:
#include <iostream.h>
#include <fstream.h>
void main()
{
int y=0,z=0;
ifstream is;
is.open("test3.txt");
char ch;
while(is.peek() != -1)
{
if (is.peek()==10 || is.peek()== -1) z++;
ch = is.get();
cout << ch;
if (is.peek() != 10 && is.peek() != -1 && is.peek() !=32) y++;
}
cout << endl;
cout <<"The number of printable characters is: "<< y << endl;
cout <<"The number of lines is: " << z << endl;
is.close();
}
I've figured out how to count the individual characters, and the lines in the file, but I am having a bit of trouble figuring out how I would count each word. Any help or pointers in the right direction would be greatly appreciated. |
|
#2
|
||||
|
||||
|
To count the number of words in the file, just read in the entire file one char at a time and count the number of whitespace characters that occur (be sure to avoid counting two in a row).
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#4
|
|||
|
|||
|
Thanks Guys
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > How to count words? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|