well my friends I tried to solve a problem and it was about allowing the user to enter a paragraph , then the compiler'll count and display the number of typed words .

welll I easily solved it like this

--------------------------------------------------

#include<iostream>
#include<string>
using namespace std;
void main()
{
	int words=0;
	string str;
	getline(cin,str);
	for(int i=0;i<(int)str.length();i++)
		if(isspace(str[i]))
			words++;
	cout<<"number of typed words is: "<<words+1<<endl;
}

-----------------------------------------------------------------------------------
but later I relized that it was very weak program
coz some users use tabs just move the pointing crosser
things like these
so who has a better idea ?

Would you mind reading this and use code tags ?

Edit:: And something else: migrate your code to standard C++ code (don't use [B]void main[/B] for example), review this web page carefully :)

Edit:: Tom Gunn's (one post below, #3) is right in saying that it will check for all space characters, look at this

For a loose definition of 'word', you can use cin:

string word;

while (cin >> word)
{
    cout << word << '\n';
}

For a stronger definition of 'word', you should do more parsing than just looking for spaces. Punctuation and stuff like that wouldn't be a part of the word, right?

edit
By the way, isspace checks for all space characters: {' ', '\t', '\r', '\n', '\v', '\f'}. Your program should work with tabs too, but you don't take multiple space characters between words into account, so the word count is wrong.

The following:

for(int i=0;i<(int)str.length();i++)
if(isspace(str[i]))
words++;

has to be:

for(int i=0;i<(int)str.length()[B]-1[/B];i++)
    if(isspace(str[i]))
        words++;

Edit:: No, this won't work either

you could use stringstream class in <sstream>

string sentence = "Now is the time for all old folks to be in bed";
string word;
stringstream str(sentence);
while( str >> word)
   cout << word << "\n";

Why not use stringstreams?

#include<iostream>
#include<string>
#include<sstream>
int main()
{
    std::string str;
    getline(std::cin,str);
    std::stringstream sstrm(str);
    int count=0;
    std::string temp;
    while(sstrm>>temp) count++;
    
    std::cout<<count<<std::enl;
}

output:

siddhant3s@Xion:~$ g++ tester.cpp -o tester.exe -ansi -pedantic && ./tester.exe
Let me type something   which 	also contains tabs
8
siddhant3s@Xion:~$

Edit: [siddhant3s wants to question the world why he every time late by few seconds. @Ancient Dragon]

Why not use stringstreams?

They add unnecessary complexity?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str;

    while (cin >> str) count++;
    
    cout << count << '\n';
}

They add unnecessary complexity?

And, with your otherwise excellent solution, you have to terminate input with Ctrl+Z instead of just <Enter>

They add unnecessary complexity?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str;

    while (cin >> str) count++;
    
    cout << count << '\n';
}

The word counting will never stop... (unless you terminate your program in an improper way)

Edit:: AD was before me :P

>They add unnecessary complexity?
Oh stop kidding......
They are simplifying the program not making it more complex. Your example won't work if you intend to write a function to count the number of words in a given string.

And, with your otherwise excellent solution, you have to terminate input with Ctrl+Z instead of just <Enter>

The problem was to allow the user to enter a paragraph. I bet there will be more than one line, and to handle multiple lines with getline you need to terminate input with something besides <Enter>.

>They add unnecessary complexity?
Oh stop kidding......
They are simplifying the program not making it more complex. Your example won't work if you intend to write a function to count the number of words in a given string.

Who's kidding? You're adding more complexity by making the code 'function ready' when Alicito's code does everything in main. Besides, if you want to write a function you can pass an istream and use the same short code without hurting flexiblity. It works with stringstreams and fstreams too:

#include <iostream>
#include <string>

using namespace std;

unsigned long CountWords(istream& stream)
{
    unsigned long count = 0;
    string str;

    while (stream >> str) ++count;

    return count;
}

int main()
{
    cout << CountWords(cin) << '\n';
}

Ancient Dragon's complaint makes more sense, but only if you're reading one line interactively.

The problem was to allow the user to enter a paragraph. I bet there will be more than one line, and to handle multiple lines with getline you need to terminate input with something besides <Enter>.

Hey great programmer, I bet you forgot something :P
Your program doesn't ignore punctuation marks, a great programmer as you would certainly have seen that, not?

e.g.:
Are these: "Hello , World !!!" four words or two ?

>>four words or two
Two. Punctuation marks are not considered words. None of the solutions presented so far in this thread would parse that sentence correctly.

Your program doesn't ignore punctuation marks, a great programmer as you would certainly have seen that, not?

I'm not a great programmer by any measure, but I did say that the definition of 'word' is important in my first post:

For a loose definition of 'word', you can use cin:

string word;

while (cin >> word)
{
      cout << word << '\n';
}

For a stronger definition of 'word', you should do more parsing than just looking for spaces. Punctuation and stuff like that wouldn't be a part of the word, right?

Salem jumped me about giving away code, so I didn't give an example of the stronger definition. Blame him. ;)

>Salem jumped me about giving away code, so I didn't give an example of the stronger definition. Blame him.
No, giving a stronger definition about the word 'word' is not the same as giving away free code :)

This article has been dead for over six months. Start a new discussion instead.