Page 1 of 1

Reversing a string

Posted: Mon Sep 22, 2008 2:40 am
by aarondb
Hey guys,

I'm new to revolution and have to figure out a program to have the user input a phrase or series of numbers and check to see if what they entered is a palindrome. Unfortunately, I don't know how to do it on Revolution.

Normally I would find the length of the string, then match up the first letter with the last letter, 2nd with the 2nd to last, etc.

Also, it seemed like there was a "reversed" code. I tried to do it but it didn't work. I also have to consider blank spaces that users may enter in the phrase.

If someone could give me a concrete code or talk me through how to do this with Revolution code, I would greatly appreciate it.

Posted: Mon Sep 22, 2008 6:27 am
by Janschenkel
To reverse a string, use the foillowing function.

Code: Select all

function ReversedString pString 
  local tIndex, tReversedString 
  repeat with tIndex = the length of pString down to 1 
    put char tIndex of pString after tReversedString 
  end repeat 
  return tReversedString 
end ReversedString
But that might be a little overkill.

Code: Select all

function IsAPalindrome pString 
  local tLength, tIndex 
  put the length of pString into tLength 
  repeat with tIndex = 1 to tLength DIV 2 
    if char tIndex of pString is not char (tLength - tIndex + 1) of pString
    then return false 
  end repeat 
  return true 
end IsAPalindrome
Hope this helped,

Jan Schenkel.