Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mit-scratch

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?

like image 579
Spike Williams Avatar asked Oct 24 '14 16:10

Spike Williams


2 Answers

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.

like image 153
Scimonster Avatar answered Sep 18 '22 13:09

Scimonster


You can do it without variables too (using the length of the current output)...

delete (all v) of [Output v]
repeat (length of (originalString))
  add (letter((length of [Output v]) + (1)) of (originalString)) to [Output v]
end

Visual Representation

like image 21
Carter Medlin Avatar answered Sep 19 '22 13:09

Carter Medlin