Reversing a string

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
aarondb
Posts: 1
Joined: Mon Sep 22, 2008 2:35 am

Reversing a string

Post by aarondb » Mon Sep 22, 2008 2:40 am

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.

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Mon Sep 22, 2008 6:27 am

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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply