Skip to main content
guest
Join | Help | Sign In
Liberty BASIC Community Wiki Home
guest| Join | Help | Sign In
Liberty BASIC Community Wiki
  • Wiki Home
  • Recent Changes
  • Pages and Files
  • Members
  • Home
    • Liberty BASIC FAQ
    • Contests
    • Tutorials
    • Science
    • Shared Code
    • Tips
    • DLLs
    • PublicDomainSprites
    • Member Pages
    • Links
    • Sandbox

GetWordCount

Edit 0 1 …
  • 0 Tags
    • No tags
  • Notify
  • RSS
  • Backlinks
  • Source
  • Print
  • Export (PDF)
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.
 [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
Help · About · Pricing · Privacy · Terms · Support · Upgrade
Contributions to https://basic.wikispaces.com/ are licensed under a Creative Commons Attribution Share-Alike 2.5 License. Creative Commons Attribution Share-Alike 2.5 License
Portions not contributed by visitors are Copyright 2018 Tangient LLC
TES: The largest network of teachers in the world
Turn off "Getting Started"
  1. Home
  2. ...
Loading...