5

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?

4

3 回答 3

6

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.

于 2014-10-25T19:16:04.400 回答
2

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

于 2015-04-13T23:02:26.363 回答
0

The simplest thing to do is to use a list and to enter letters one by one. There is a good library for lists in scratch 3.

If this is not possible, due to change in the intended behavior of the program, then you could put the string, letter by letter, into a list of letters, by using the "Operators" commands - on strings. After that you could do anything with your list of letters, using the "Variables" commands - for lists.

I hope it is clear enough :)

于 2020-04-05T21:24:59.167 回答