I'm wondering how it would work in scratch, that if you type in a word, it reads the word and detects whether it's a Palindrome or not. Should work for any length word.
Thanks in advance
Offline
I have an idea. Would it be ok if the person had to type it in backwards or forwards? Because if so, then this would work:
[blocks]<if><( <{ Backwards }> <=> <{ Forwards }> )>
<broadcast[ Palindrome
<else>
<broadcast[ No Palindrome[/blocks]
Last edited by PlutoIsHades (2011-09-13 06:35:40)
Offline
Hm... well, you can use the length of( ) block to find the length of the word. Then just scan through it letter by letter, adding each letter to a list. Once you get to the center of the word, (making sure to disregard the letter at the very middle if there are an odd number of letters) start checking if each letter is the same as the one stored in the list in reverse order. That should tell you if it's a palindrome.
Just one caveat: make sure to have a script run through and remove all the spaces before running the script I described above. Otherwise, any multi-word palindrones (like race car) won't work.
Hope that helps!
Offline
Here's little project with an algorithm to detect palindromes - I'll leave it to you to tweak it for multi-word palindromes
http://scratch.mit.edu/projects/Ratty1967UK/2030437
Offline