Hi all,

I want to find the number of words in a rich edit control. There is no direct class member to do this. So I think if I can count the number of word break and line brake then it is easy. But it is also not easy. On the MSDN also it is not well documented. Can you guys give me a clue.

Thanks.

Recommended Answers

All 11 Replies

Hi all,

I want to find the number of words in a rich edit control. There is no direct class member to do this. So I think if I can count the number of word break and line brake then it is easy. But it is also not easy. On the MSDN also it is not well documented. Can you guys give me a clue.

Thanks.

If your RichEdit control is on a dialog box call GetDlgItemText and then count the number of spaces in the lpString.
You can also use EM_GETTEXTRANGE message if you know the handle of the control to retrive the text and then count number of white space.

Number of Words = Number of space + 1

Member Avatar for iamthwee


Number of Words = Number of space + 1

In    that         case   this   sentence    has 27 words.
In    that         case   this   sentence    has 27 words.

@ iamthwee Thanks! :)

@eranga262154 Once you get the string, counting the number of words should not be a big problem.

"Number of Words = Number of space + 1" is focusing simplest case. However, be careful you may have audience and users like iamthwee (no offence), who may spoil your algorithm.

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;
commented: I approve. +13
commented: me too =D +3

Number of Words = Number of space + 1

Yes that's what I'm going to do. Find the number of spaces, tabs, carriage returns of the string. I can get the string from the rich edit control. How about the following attempt.

bool isLastCharBlank = true;
	int iWordCount = 0;
 
	char * szTemp = szInputString; 
 
	while(*szTemp) 
	{
		// Whitespase, tab, newline and carriage return
 		if ((*szTemp == ' ') || (*szTemp == '\n') || (*szTemp == '\r')) 
		{
			isLastCharBlank = true;
		}
 
		else if (isLastCharBlank) 
		{
 			iWordCount++;
			isLastCharBlank = false;
		}
 		szTemp++;
	}
	printf("Number of words %d",iWordCount);
In    that         case   this   sentence    has 27 words.

That is one of the big problem I've got. Multiple such instances(like spaces and tab, two spaces, and such..). So please look at my code, how I try to avoid it.

Did you see my code snippet? Why can't you use that?

std::stringstream ss(line);

Yes, I check your code as well. But is this valid. Got an compile error.

have you included the header file? #include <sstream>

Yes, I use it but not works.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.