Search code examples
mit-scratch

In Scratch, how can you split up a string into a list of characters?


My son is interested in the ROT-13 cypher. I would like to help him write a program in MIT Scratch that can take a string as input and return the ROT-13 encoded text as output. To do this, the program will need to take the string, separate out all the characters, change the characters according to the ROT-13 cypher, and reassemble them back into the string.

I recognize that Scratch isn't really built for string manipulation, but its the programming environment that my son understands. Is this kind of string manipulation feasible in Scratch? If so, how would it be done? To get started, how would you split up a string into the equivalent of an array of characters?


Solution

  • The best way to do this is by simple iteration.

    set [i v] to (0)
    repeat (length of (originalString))
      change [i v] by (1)
      add (letter (i) of (originalString)) to [characters v]
    end
    

    (Visual representation)

    The list characters will contain each character of the original string.