943,892 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5315
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 17th, 2007
0

Word break and line break in Rich Edit Control

Expand Post »
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.
Similar Threads
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Dec 17th, 2007
0

Re: 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.
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
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 17th, 2007
0

Re: Word break and line break in Rich Edit Control


Number of Words = Number of space + 1

C++ Syntax (Toggle Plain Text)
  1. In that case this sentence has 27 words.
Last edited by iamthwee; Dec 17th, 2007 at 9:51 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 17th, 2007
0

Re: Word break and line break in Rich Edit Control

Click to Expand / Collapse  Quote originally posted by iamthwee ...
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 17th, 2007
2

Re: Word break and line break in Rich Edit Control

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.

cpp Syntax (Toggle Plain Text)
  1.  
  2. #include <string>
  3. #include <sstream>
  4.  
  5. // Get the line from the Rich Edit Control .
  6. // Let's hard code it for this example.
  7. std::string line = "In that case this sentence has 27 words.\r\nThis is another line";
  8. std::stringstream ss(line);
  9. std::string word;
  10. int count = 0;
  11.  
  12. while ( ss >> word ){
  13. std::cout << word << std::endl;
  14. count++ ;
  15. }
  16. std::cout << count << std::endl;
Last edited by WolfPack; Dec 17th, 2007 at 10:23 am. Reason: Included the header files
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Dec 17th, 2007
0

Re: Word break and line break in Rich Edit Control

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.

C++ Syntax (Toggle Plain Text)
  1. bool isLastCharBlank = true;
  2. int iWordCount = 0;
  3.  
  4. char * szTemp = szInputString;
  5.  
  6. while(*szTemp)
  7. {
  8. // Whitespase, tab, newline and carriage return
  9. if ((*szTemp == ' ') || (*szTemp == '\n') || (*szTemp == '\r'))
  10. {
  11. isLastCharBlank = true;
  12. }
  13.  
  14. else if (isLastCharBlank)
  15. {
  16. iWordCount++;
  17. isLastCharBlank = false;
  18. }
  19. szTemp++;
  20. }
  21. printf("Number of words %d",iWordCount);
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Dec 17th, 2007
0

Re: Word break and line break in Rich Edit Control

Click to Expand / Collapse  Quote originally posted by iamthwee ...
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Dec 18th, 2007
0

Re: Word break and line break in Rich Edit Control

Did you see my code snippet? Why can't you use that?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Dec 18th, 2007
0

Re: Word break and line break in Rich Edit Control

Click to Expand / Collapse  Quote originally posted by WolfPack ...
cpp Syntax (Toggle Plain Text)
  1.  
  2.  
  3. std::stringstream ss(line);
Yes, I check your code as well. But is this valid. Got an compile error.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Dec 18th, 2007
0

Re: Word break and line break in Rich Edit Control

have you included the header file?

#include <sstream>
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help regarding MSI Serial Key Validation
Next Thread in C++ Forum Timeline: I need help please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC