A function that returns the number of "words" within a given string as determined by a specified delimiter.
Test program included. See comments inside function for explanation.
[loop]
input "Enter a string: "; s$
ifnot(s$ ="")then[delim]
input "Enter delimiter: "; d$
ifnot(d$ ="")then
count = GetWordCount(s$,d$)
print
print chr$(34); s$; chr$(34); " contains "; count;_
" word"; word$("s", abs(not(count =1))); _
" as delimited by "; chr$(34); d$; chr$(34)
print : print
elsegoto[delim]endifelseendendifgoto[loop]function GetWordCount(aString$,delim$)'Remove any trailing and leading spaces'from source string. (Will also blank the'string if it contains nothing but spaces.)
aString$ =trim$(aString$)'If the source string and delimiter parameter'are not blank...if(not(aString$ ="")andnot(delim$ =""))then'Then there is at least one "word",'so increment the counter.
GetWordCount = GetWordCount +1'Continuously:do'Check for the next instance of the delimiter.
a =instr(aString$, delim$, a +1)'If another instance found, increment counter.if(a)then GetWordCount = GetWordCount +1loopwhile a
endifendfunction
GetWordCount function
A function that returns the number of "words" within a given string as determined by a specified delimiter.