Small basic coding Arrays Letterstonumbers

47 Views Asked by At

I’ve run into a problem in Smallbasic where I need to convert letters to numbers and then add a “shift” to them (with a number from the user) and then apply the shift and convert it back to a encrypted message. Does anyone know how to solve this problem because I’m not so good with arrays and encoding.

1

There are 1 best solutions below

5
Mohamed Elgazar On

One way to do the loop would be to use a For loop, and iterate through each letter of the message. For each letter, you would call the function that shifts the letter by the appropriate amount.

Here is some example code that would do what you are describing:

alphabet = "abcdefghijklmnopqrstuvwxyz"

message = "this is a test message"

shift = 3

For i = 1 to Len(message)

letter = Mid(message, i, 1)

shiftedLetter = ShiftLetter(letter, shift)

Print(shiftedLetter)

EndFor

Function ShiftLetter(letter, shift)

letterIndex = InStr(alphabet, letter)

shiftedIndex = letterIndex + shift

If shiftedIndex > Len(alphabet) Then

shiftedIndex = shiftedIndex - Len(alphabet)

EndIf

shiftedLetter = Mid(alphabet, shiftedIndex, 1)

Return shiftedLetter

EndFunction