Older Version Newer Version

bshipps64 bshipps64 Mar 5, 2006 - "First created"

== GetWordCount function ==
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.
[[code format="vbnet"]]
[loop]
    input "Enter a string: "; s$
    if not(s$ = "") then
[delim]
        input "Enter delimiter: "; d$
        if not(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
        else
            goto [delim]
        end if
    else
        end
    end if
    goto [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$ = "") and not(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 + 1
        loop while a
    end if
end function
[[code]]