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

3

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?

Hansen answered 24/10, 2014 at 16:37 Comment(0)
B
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.

Bertrambertrand answered 25/10, 2014 at 19:16 Comment(1)
You could put that into a "run without screen refresh" function to make it run (almost) instantly.Shornick
M
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

Michaels answered 13/4, 2015 at 23:2 Comment(0)
C
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 :)

Combes answered 5/4, 2020 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.