RosettaCodeData/Task/Phrase-reversals/Run-BASIC/phrase-reversals.basic
2024-10-16 18:07:41 -07:00

54 lines
2 KiB
Text

sString$ = "rosetta code phrase reversal" 'The string
sNewString$ = "" 'string variables
sTemp$ = "" 'Temporary string variable
siCount = 0 'Counter
dim sWord$(100) 'Word store
i = 0 'Index variable
startPos = 1 'Start position for word extraction
endPos = 0 'end position for word extraction
wordCount = 0 'Number of words
' Loop backwards through the string
for siCount = len(sString$) to 1 step -1
sNewString$ = sNewString$ + mid$(sString$, siCount, 1) 'Add each character to the new string
next
print "Original string => " + sString$ 'Print the original string
print "Reversed string => " + sNewString$ 'Print the reversed string
sNewString$ = "" 'Reset sNewString
' Manually split the original string by spaces
for i = 1 to len(sString$)
if mid$(sString$, i, 1) = " " or i = len(sString$) then
if i = len(sString$) then
endPos = i
else
endPos = i - 1
end if
sWord$(wordCount) = mid$(sString$, startPos, endPos - startPos + 1)
wordCount = wordCount + 1
startPos = i + 1
end if
next
' Loop backward through each word in sWord
for siCount = wordCount - 1 to 0 step -1
sNewString$ = sNewString$ + sWord$(siCount) + " " 'Add each word to sNewString
next
print "Reversed order => " + sNewString$ 'Print reversed word order
sNewString$ = "" 'Reset sNewString
' For each word in sWord
for i = 0 to wordCount - 1
sTemp$ = sWord$(i)
' Loop backward through the word
for siCount = len(sTemp$) to 1 step -1
sNewString$ = sNewString$ + mid$(sTemp$, siCount, 1) 'Add the characters to sNewString
next
sNewString$ = sNewString$ + " " 'Add a space at the end of each word
next
print "Reversed words => " + sNewString$ 'Print words reversed