-1

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.

  • There are a few ways to approach this problem. One way would be to create an array of the alphabet, and then use a loop to iterate through each letter of the message, shifting it by the appropriate amount. Another way would be to create a function that takes a letter and a shift amount as parameters, and then returns the shifted letter. – Mohamed Elgazar Oct 16 '22 at 15:31
  • I’ve made the alphabet, but I don’t know how to do the loop you mentioned. – Teach me the way Oct 16 '22 at 15:34

1 Answers1

0

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
Mohamed Elgazar
  • 778
  • 4
  • 9
  • I can follow some of this, but could you explain this part? For i = 1 to Len(message) letter = Mid(message, i, 1) shiftedLetter = ShiftLetter(letter, shift) – Teach me the way Oct 16 '22 at 15:46
  • The code is saying that for each letter in the message, it should call the ShiftLetter function with the letter and the shift amount as parameters. The function will then return the shifted letter, which will be printed out. – Mohamed Elgazar Oct 16 '22 at 15:48
  • for smallbasic, the mid is recognized As a variable and not a function. What is the mid? (Mid(message, i, 1)) – Teach me the way Oct 16 '22 at 16:03
  • The Mid function returns a substring of a string. The first parameter is the string to get the substring from, the second parameter is the starting index of the substring, and the third parameter is the length of the substring. So, for example, if you have a string "Hello World", and you call Mid("Hello World", 1, 5), it will return "Hello". – Mohamed Elgazar Oct 16 '22 at 16:05
  • When I tested that, it showed an error saying that mid was a subroutine – Teach me the way Oct 16 '22 at 16:09