Chapter: Modifiers & Structure (IV)
Program: Chapter Program "Palindromotron":
Here is the full Code as presented in the pdf:
Code: Select all
Again:
CLS
COLOR 15, 1
PRINT “==================================”
PRINT “ =========== ===========”
PRINT “ === Palindromotron ===”
PRINT “ =========== ===========”
PRINT “==================================”
COLOR 7, 0
PRINT “This program is capable of reversing”
PRINT “words, sentences or entire texts.”
DIM TheString AS STRING, EndString AS STRING
INPUT “Please enter some:”, TheString
TheString = LTRIM$(RTRIM$(TheString))
IF LEN(TheString) = 0 THEN GOTO Again
GOSUB reversestring
PRINT “This is it, reversed: ”; EndString
PRINT
EnterAgain:
PRINT “Do you want to enter some text again? (y/n)”
INPUT “(y/n):”, choice$
Choice$ = LTRIM$(RTRIM$(UCASE$(choice$)))
IF Choice$ = “Y” THEN GOTO Again
IF Choice$ = “N” THEN END
‘the program will only get here if Choice$ ≠ “Y” AND Choice$ ≠ “N”
GOTO EnterAgain
Reversestring:
‘this function can actually be made much better using FOR or WHILE
‘but since you haven‘t had them yet, I do it with GOTO
NextChar:
CurrentPos = LEN(TheString)
EndString = EndString + MID$(TheString, CurrentPos, 1)
CurrentPos = CurrentPos – 1 ‘decrease CurrentPos with 1
IF CurrentPos <> 0 THEN GOTO NextChar
RETURN
Fix: Set CurrentPos only once, before the NextChar sub-routine:
Code: Select all
CurrentPos = LEN(TheString)
NextChar:
EndString = EndString + MID$(TheString, CurrentPos, 1)
CurrentPos = CurrentPos – 1 ‘decrease CurrentPos with 1
IF CurrentPos <> 0 THEN GOTO NextChar