word count

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
Hey, I'd like some help creating a word count for my program. I can count characters, and even lines I believe, but not words. I think I need something that counts all the spaces in the text and uses that to find out how many words there are. Any suggestion is appreciated
 
Counting the spaces is not a bad idea, check all characters in string and count the space ones.
 
Count the spaces and use that number. Just be sure to do some processing so that spaces back to back don't count. And then, in the end, just let the user know the count is approximate like Word does (did?).
 
Okay, So my Idea to look for the spaces is a good one, but I'm having trouble doing it. Am I supposed to be using me.richtextbox.find( ) ? Whenever I have used this before, It only finds the first word to match, and just stops. How would I use it to find all the matching words, or in this case spaces, and return the number found as an integer?
 
Okay, tell me if I'm way off here. What I'm trying to do is loop the indexof until it = -1. For each loop it adds 1 to my TotalWords Variable. The only problem is, I get stuck in the loop. For some reason it never equals -1. I think the problem is that it keeps finding the same space. Do I need to somehow change the start integer to be where it found the last space? As in, once it has been found i exclude it from the search? If so, how would I go about doing this?
 
I did it!!! Here is the code:

VB.NET:
first = Me.RichTextBox1.Text.IndexOf(" ")
total += 1
 
Do Until Me.RichTextBox1.Text.IndexOf(" ", first + 1) = -1
first = Me.RichTextBox1.Text.IndexOf(" ", first + 1)
total += 1
 
Loop
of course this only counts spaces, so it is approximate. And also, as of this moment it counts consecutive spaces, so it's not that great.
 
Last edited by a moderator:
Back
Top