Number of Words = Number of space + 1
In that case this sentence has 27 words.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Something like this will be easy. There are more compilicated methods if you want to break Japanese or Chinese words. Also you can use a callback function to count the words on the fly, but I think this is the most simple way.
#include <string>
#include <sstream>
// Get the line from the Rich Edit Control .
// Let's hard code it for this example.
std::string line = "In that case this sentence has 27 words.\r\nThis is another line";
std::stringstream ss(line);
std::string word;
int count = 0;
while ( ss >> word ){
std::cout << word << std::endl;
count++ ;
}
std::cout << count << std::endl;
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Did you see my code snippet? Why can't you use that?
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
have you included the header file?
#include <sstream>
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
If including sstream doesn't give an error, but using std::stringstream does, then something else is wrong. What is the compile error? And what is the compiler you are using?
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115