Reversing text

This lesson will explain how to reverse a piece of text.

The reversed function

The reversed function takes one parameter, pText, reverses it character by character and returns the backwards version of the original string.

get reversed("A man, a canal, Panama")
put reversed(it) into field "Reversed Text"
answer reversed(field "Answer")

The handler uses a repeat control structure to scan each character in the string. Unlike a typical repeat loop, this one doesn't start at the first character and go to the last. Instead, it uses the repeat...down to form to start at the last character and go back to the first.

The handler places each character at the end of a variable called tReversedString. Because the repeat loop in this handler goes backwards, the tReversedString variable ends up holding the same characters as the original string, but in reverse order. When the repeat loop finishes, the tReversedString variable is returned as the value of the function.

You can use similar handlers to reverse the words or lines in a string. In fact, using the itemDelimiter property, you can reverse the items in a string, using any character as the item delimiter.

The reversed function code

function reversed pString
	local tReversedString
   
	repeat with tChar = the length of pString down to 1
		put char tChar of pString after tReversedString
	end repeat
	
	return tReversedString
end reversed

A note on efficiency

You might initially think that using the repeat for each form of the repeat control structure would be faster, since in general, this form takes much less time than the repeat with x = firstValue to lastValue form:

repeat for each character tChar in pString
	put tChar before tReversedString
end repeat

However, this turns out to be slower in this particular case. The reason is that it takes longer to put a string before a variable than to put a string after a variable. Since we need to reverse the string, and since the repeat for each form doesn't have a way to go backwards, we have no choice but to put each character before the tReversedString variable. The speed loss from doing this is much greater for this handler than the speed we'd gain from using the repeat for each form.

Keywords used in this example

2 Comments

Glenn Webster

Would it be possible to post a script example of reversing words in a line of a field using itemdelimiter as mentioned '...You can use similar handlers to reverse the words or lines in a string. In fact, using the itemDelimiter property, you can reverse the items in a string, using any character as the item delimiter...'

Panos Merakos

Hello Glenn,

This is an example of reversing the words in a string, using the itemDelimiter property:

function reversed2 pString
local tReversedString

set the itemDelimiter to " "
local tTotal
put the number of items of pString into tTotal

repeat with tWord = tTotal down to 1
put item tWord of pString & " " after tReversedString
end repeat

return tReversedString
end reversed2

if you now call:

put reversed2("this is a text to reverse")

you will get:

reverse to text a is this

Hope this helps.

Kind regards,
Panos
--

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.