954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Word break and line break in Rich Edit Control

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.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

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

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 
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
 
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.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

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
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
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);
eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 
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.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

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

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
std::stringstream ss(line);

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

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

have you included the header file?

#include <sstream>

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Yes, I use it but not works.

eranga262154
Junior Poster
185 posts since Sep 2007
Reputation Points: 32
Solved Threads: 2
 

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
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You