Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

space count ! Help

I need help on how to count the spaces in a paragraph or text in a text box.
i think you have to use strings..but give me your opinion and tips.
I would appreciate it if you just give me the code :D , but tips will work too :D
thx in advance.
[255 byte] By [kcin19] at [2007-11-11 10:28:12]
# 1 Re: space count ! Help
Use this code.

txt = "This is Riz writing the code :)"
SpaceCount = 0
for x = 1 to len(txt)
if Mid(txt,x,1)=" " then SpaceCount = SpaceCount + 1
next
msgbox SpaceCount & " spaces found."
Riz at 2007-11-11 17:22:39 >
# 2 Re: space count ! Help
Here's a tricky way to do it:

Dim LengthWithSpaces As Integer
Dim LengthWithoutSpaces As Integer
Dim NumberOfSpaces As Integer
Dim Text As String

LengthWithSpaces = Len(Text)
Text = Replace(" ", "")
LengthWithoutSpaces = Len(Text)
NumberOfSpaces = LengthWithSpaces - LengthWithoutSpaces
Phil Weber at 2007-11-11 17:23:38 >
# 3 Re: space count ! Help
You guys really helped, thx again.
What i am trying to make is a word counter, and i thought the only way to count the words are to count the spaces between the words, so thats why i asked how to count the spaces.

I made the word counter, but theres only 1 problem :confused: , it counts the spaces too. :eek: For example if i type 3 spaces and no words, it says there are 3 words... lol
so now i am just trying to make this "word counter" into an actual word counter. :)
kcin19 at 2007-11-11 17:24:37 >
# 4 Re: space count ! Help
I think this might work. Try this I haven't tested it :)

txt = "This is Riz writing the code"
WordCount = 0
WordStarted = false
InSideWord = true
for x = 1 to len(txt)
if Mid(txt,x,1)<>" " then
WordStarted = true
InSideWord = false
end if
if WordStarted and InSideWord=false then
WordCount = WordCount + 1
InSideWord = true
end if
next
msgbox WordCount & " words found."
Riz at 2007-11-11 17:25:39 >
# 5 Re: space count ! Help
I think this might work. Try this I haven't tested it :)
THe program seems to be counting the characters, not words. but it does'nt count the spaces anymore :)
kcin19 at 2007-11-11 17:26:37 >
# 6 Re: space count ! Help
Try this:

Function WordCount(ByVal Text As String) As Integer

Dim MultiSpaces As Boolean
Dim Words() As String

Text = Trim(Text)
Do
Text = Replace(Text, " ", " ")
MultiSpaces = InStr(Text, " ")
Loop While MultiSpaces

Words = Split(Text)
WordCount = UBound(Words) + 1

End Function
Phil Weber at 2007-11-11 17:27:47 >
# 7 Re: space count ! Help
Try this:

Function WordCount(ByVal Text As String) As Integer

Dim MultiSpaces As Boolean
Dim Words() As String

Text = Trim(Text)
Do
Text = Replace(Text, " ", " ")
MultiSpaces = InStr(Text, " ")
Loop While MultiSpaces

Words = Split(Text)
WordCount = UBound(Words) + 1

End Function

I'm afraid i dotn know how to use the "Function" yet, so i dont understand the above codes, please use strings if possible. Thx.
I got to the point where everything works fine except, the program counts the extra spaces between words.
kcin19 at 2007-11-11 17:28:42 >
# 8 Re: space count ! Help
"Phil Weber" has given very good suggestion.
To use it simply copy the function in your code and use it like this

c = WordCount("This is my Text")
msgbox c & " words found."
Riz at 2007-11-11 17:29:45 >
# 9 Re: space count ! Help
Hi EveryBody...
As you are checking for single space check for double space.

for i = 1 to len(txt)
if mid(txt,i,1)=" " and mid(txt,i+1,1)<>" " Then
i = i + 1
end if
next

i = i + 1 is the number of words.
hope this helps you.

Regards
AJ...
aj_vbp at 2007-11-11 17:30:42 >
# 10 Re: space count ! Help
Hi EveryBody...
As you are checking for single space check for double space.

for i = 1 to len(txt)
if mid(txt,i,1)=" " and mid(txt,i+1,1)<>" " Then
i = i + 1
end if
next

i = i + 1 is the number of words.
hope this helps you.

Regards
AJ...
Thank you AJ, but my program needs to be able to not count double spaces and as well as more than 2 spaces. so if you type : "THis is kcin." The program should show that you have 3 words.
kcin19 at 2007-11-11 17:31:43 >
# 11 Re: space count ! Help
dim d() as string
d = split(myText, " ") '' <- only one space!
dim k as long
dim count as long
for k = 0 to ubound(d)
if len(d(k)) > 0 then count = count+1
next

split() returns an array splitting the string at every space, so it is possible to get empty words when there are more then ona space in between words, this is why I check for the length of the word to increase the word counter. The code works fine for any sequence of spaces, BUT:

this code works ONLY with spaces, and NOT if the words are separated by TAB o Return. In that case, one way is to scan the string char by char
mstraf at 2007-11-11 17:32:49 >
# 12 Re: space count ! Help
Exactly the one i have given will work and give what u want. just in place i=i+1 u need to write wordcount = wordcount + 1.

this will work the way u required.

wordcount = 1

for i=1 to len(txt)
if ( mid(txt,i,1)=" " and mid(txt,i+1,1)<>" " then
wordcount=wordcount + 1
end if
next

wordcount will display exactly what u want.

Hello how are you? return 4 words.
Hello World will return 2 words.
Hello will return 1 word.

regards
AJ
aj_vbp at 2007-11-11 17:33:46 >
# 13 Re: space count ! Help
actually, if the string is ""Hello how are you? " (any tailing spaces) the output is 5 and not 4, as well as the string " Hello how are you?" (any trailing spaces)
To fix this problem it is sufficient to Trim() the input string:
txt = Trim(txt)

Marco
mstraf at 2007-11-11 17:34:44 >
# 14 Re: space count ! Help
Thx for all your help guys.
You guys are the best :)
Love You All .
kcin19 at 2007-11-11 17:35:50 >
# 15 Re: space count ! Help
hi mstraf,

I am talking about a string where there is more than one spaces between two words. i.e incase string "Hello World" is written with mulitple spaces between them. words are still 2, but with multiple spaces between them. Trim won't get rid off such spaces. To deal them the logic specified by me is capable to handle this situation.

Hope this time I am clear. One notified by you in example will work for all leading and trailing spaces. but what about spaces in middle of words.

Regards
AJ
aj_vbp at 2007-11-11 17:36:53 >
# 16 Re: space count ! Help
of course your code works fine with any number of spaces in between words.
my fix (using Trim) was only for cases when there are also trailing and/or tailing spaces, because in that case your code gives a bigger count.

Regards,
Marco
mstraf at 2007-11-11 17:37:53 >