' getword.bas ' 1999 by Mercury0x000D ' updated 2005 by mercury0x000d ' updated 2018 by mercury0x0d DEFINT A-Z CLS a$ = " it's a secret to everybody " sep$ = " " PRINT "The string is: '"; a$; "'" PRINT "The separator is: '"; sep$; "'" CALL GetWordCount(a$, sep$, words) PRINT "The string has"; STR$(words); " words:" FOR a = 1 TO words CALL GetWord(a$, sep$, a, w$) PRINT "Word"; STR$(a); ": '"; w$; "'" NEXT SUB GetWord (inputText$, seperatorChar$, wordRequested, wordReturned$) ' see if we need to exit immediately IF inputText$ = "" THEN EXIT SUB IF seperatorChar$ = "" THEN EXIT SUB wordCount = 0 lastType = 0 FOR a = 1 TO LEN(inputText$) b$ = MID$(inputText$, a, 1) IF b$ = seperatorChar$ THEN ' if we have the word requested, then exit! IF wordCount = wordRequested THEN EXIT SUB ' if we get here, it wasn't the proper word wordReturned$ = "" lastType = 2 ELSE ' make sure the last character wasn't a non-separator also IF lastType <> 1 THEN wordCount = wordCount + 1 lastType = 1 ' add this character to the word to possibly be returned wordReturned$ = wordReturned$ + b$ END IF NEXT ' make sure we return null if the word requested is out of range IF wordRequested <> wordCount THEN wordReturned$ = "" END SUB SUB GetWordCount (inputText$, seperatorChar$, wordCount) ' see if we need to exit immediately IF inputText$ = "" THEN EXIT SUB IF seperatorChar$ = "" THEN EXIT SUB wordCount = 0 lastType = 0 FOR a = 1 TO LEN(inputText$) b$ = MID$(inputText$, a, 1) IF b$ = seperatorChar$ THEN lastType = 2 ELSE IF lastType <> 1 THEN wordCount = wordCount + 1 lastType = 1 END IF NEXT END SUB