Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do you reverse text?
- ShawnFeatherly
-
Scratcher
7 posts
How do you reverse text?
I googled to find out. Link #2 was https://scratch.mit.edu/discuss/topic/331755/?page=1 but I didn't like how that answer had two variables. I found the output variable had a length that matched the counter variable. Which allows for removing the second variable.
I'm unable to add a post to that thread. So I'm making a new thread and hoping the next person googling it the best of luck.
I'm unable to add a post to that thread. So I'm making a new thread and hoping the next person googling it the best of luck.
define reverse(string)
set (my variable) to []
repeat until <(length of (my variable)) = (length of (string))>
set (my variable) to (join (letter ((length of (my variable)) + (1)) of (string)) (my variable))
end
- THEPiG22
-
Scratcher
29 posts
How do you reverse text?
This is really genius code
This screen shot is for anyone having problems or can't see it right

This screen shot is for anyone having problems or can't see it right

- mmhmBeans
-
Scratcher
500+ posts
How do you reverse text?
I mean, technically you shouldn't create guides like this. However, you can create a wiki article (under your wiki profile) if you have a wiki account.
- ShawnFeatherly
-
Scratcher
7 posts
How do you reverse text?
Thanks @THEPiG22! My son got around to copying the code today and had trouble with the coloring. Likely the comment on the empty string helped him out as well. Your screenshot was perfect!
…you shouldn't create guides like this. … create a wiki article …Thank you for the suggestion. I was trying to maintain whatever format led me to that initial answer to increase the chance the next person googling it could find it. I was annoyed I couldn't just add to that thread. Suppose it makes sense wiki's are a suggested alternative to necro.
- Spentinium
-
Scratcher
100+ posts
How do you reverse text?
I did some testing and it turns out that this is the fastest code:
I did 200,000 tests of strings from 20 to 72 characters long (example: .r'kR\fDBXqQ4uv9gL6ykqEy;24K6T;uzQSy,FL)
The first method did it in 13403 ms (13.403s)
The second method did it in 14479 ms (14.479s)
The third method did it in 12263 ms (12.263s)
define reverse text (str)
set [output v] to []
repeat (length of (str))
set [output v] to (join (letter ((length of (output)) + (1)) of (str)) (output))
end
I did 200,000 tests of strings from 20 to 72 characters long (example: .r'kR\fDBXqQ4uv9gL6ykqEy;24K6T;uzQSy,FL)
The first method did it in 13403 ms (13.403s)
define reverse text (your text)
set [count v] to [1]
set [text v] to []
repeat (length of (your text))
set [text v] to (join (letter (count) of (your text)) (text))
change [count v] by [1]
end
The second method did it in 14479 ms (14.479s)
define reverse(string)
set (my variable) to []
repeat until <(length of (my variable)) = (length of (string))>
set (my variable) to (join (letter ((length of (my variable)) + (1)) of (string)) (my variable))
end
The third method did it in 12263 ms (12.263s)
define reverse text (str)
set [output v] to []
repeat (length of (str))
set [output v] to (join (letter ((length of (output)) + (1)) of (str)) (output))
end
- THEPiG22
-
Scratcher
29 posts
How do you reverse text?
I did some testing and it turns out that this is the fastest code:define reverse text (str)
set [output v] to []
repeat (length of (str))
set [output v] to (join (letter ((length of (output)) + (1)) of (str)) (output))
end
I did 200,000 tests of strings from 20 to 72 characters long (example: .r'kR\fDBXqQ4uv9gL6ykqEy;24K6T;uzQSy,FL)
The first method did it in 13403 ms (13.403s)define reverse text (your text)
set [count v] to [1]
set [text v] to []
repeat (length of (your text))
set [text v] to (join (letter (count) of (your text)) (text))
change [count v] by [1]
end
The second method did it in 14479 ms (14.479s)define reverse(string)
set (my variable) to []
repeat until <(length of (my variable)) = (length of (string))>
set (my variable) to (join (letter ((length of (my variable)) + (1)) of (string)) (my variable))
end
The third method did it in 12263 ms (12.263s)define reverse text (str)
set [output v] to []
repeat (length of (str))
set [output v] to (join (letter ((length of (output)) + (1)) of (str)) (output))
end
Just use repeat with screen refresh and it will always be instant
- Spentinium
-
Scratcher
100+ posts
How do you reverse text?
I did some testing and it turns out that this is the fastest code:
-snip-
Just use repeat with screen refresh and it will always be instant
I don't think you understand what I'm doing. I'm comparing the speeds without screen refresh and with turbo mode. Computers aren't infinitely fast, after all.
- ShawnFeatherly
-
Scratcher
7 posts
How do you reverse text?
I did some testing and it turns out that this is the fastest code:define reverse text (str)… did it in 12263 ms (12.263s)
set [output v] to []
repeat (length of (str))
set [output v] to (join (letter ((length of (output)) + (1)) of (str)) (output))
end
[]Tracking a count variable[] did it in 13403 ms (13.403s)
[]Ops method[] did it in 14479 ms (14.479s)
That's awesome. Thanks for coming up with the optimization and these tests!
The replacement on the loop makes sense;
(length of (string))
is faster than
<(length of (my variable)) = (length of (string))>
It is interesting tracking a second variable, count, slows it down less than running an extra “length of variable”.
Last edited by ShawnFeatherly (July 21, 2023 18:10:09)
- Spentinium
-
Scratcher
100+ posts
How do you reverse text?
It is interesting tracking a second variable, count, slows it down less than running an extra “length of variable”.In Scratch, setting or changing a variable takes a lot of time compared to operations such as +, -, length of, etc.
- Discussion Forums
- » Help with Scripts
-
» How do you reverse text?



