January 1, 2005

WinTask - WordCount

'
' WordCount - Count the number of words in a string

' Author: Joe Strazzere

'

a$="This is a test of the early warning system, here.  How well does it work?"

a$=Trim$(a)
i=1

pos=instr(a$," ")

While pos > 0
    i=i+1
    a$=Ltrim$(Mid$(a$,pos,Len(a$)))
    pos=instr(a$," ")
Wend

msgbox(str$(i))

 

Note that this is NOT the same as the value returned by the SplitIntoArray() function.

The WordCount snippet doesn't care if there are many spaces between words, while SplitIntoArray() does.

 

'
' Compare WordCount and SplitIntoArray
'
DIM testarray$(100)


a$=InputBox$("Enter a string", "Compare WordCount and SplitIntoArray", " This is just-a-test     ")
'a$="This  is      a test of the early warning system, here.   How well does it work?"
i=1
b$=a$

a$=Trim$(a$)
pos=instr(a$," ")

While pos > 0
   i=i+1
   a$=Ltrim$(Mid$(a$,pos,Len(a$)))
   pos=instr(a$," ")
Wend


valu=SplitIntoArray(b$,testarray$())

msgbox("["+b$+"]"+CRLF+CRLF+"WordCount sees "+str$(i)+" words."+CRLF+CRLF+"SplitIntoArry sees "+str$(valu)+" words.")