How to build a Word Splitter on the C64 in Commodore BASIC

Published: March 29, 2018, 1:45 p.m.

b'
\\nIn this episode I’m demonstrating how to build a word splitter on the Commodore 64. We’ll use string functions to parse a sentence and split each word off into an array of words so that they can be analysed later (for example, as part of an adventure game).
\\nHere’s the code I’m building:
\\n20 input a$
\\n30 gosub 100
\\n40 print:print wd;" words:"
\\n50 for i=1 to wd
\\n60 print wd$(i)
\\n70 next
\\n99 end
\\n100 rem word splitter
\\n110 lt$="":wd=1
\\n120 for i=1 to len(a$)
\\n130 lt$=mid$(a$,i,1)
\\n140 if lt$=" " then wd=wd+1:next
\\n150 wd$(wd)=wd$(wd)+lt$
\\n160 next
\\n199 return
\\nThe interesting part starts in line 100 and onwards, where I’m building a subroutine that deals with the string functions. In line 110 I’m resetting/initialising two of the three important variables: LT$ holds a single letter from the phrase we’re getting in A$, while WD is counting each word we’re splitting out.
\\nThe FOR loop in line 120 parses each letter of the phrase, and if it finds a space character (line 140), the word count is increased. If the letter is not a space, then it’s added to the current word held in WD$(WD). The current word is assembled character by character.
\\nApologies for the audio quality, I did this on my laptop while sitting on the balcony, hence sea planes flying overhead can be heard (as well as the neighbours dog and kids).
\\nHappy hacking \\U0001f642
'