top of page

56. Reverse Text


Inverts a string of text or a text variable (reverses it).


// Set up the loop
SET Temp-n TO LENGTH( TextVar )
SET LoopLimit-n[ Temp-n ] TO 1

Temp-n - COUNTER + 1,
1 )

// Reset the variables
SET LoopLimit-n[ Temp-n ] TO UNANSWERED


This computation takes a text variable or a string of text and reverses it. For example, "mississippi" is returned as "ippississim."

This can be a useful tool for extracting values from strings. For example, if you wished to take the filename portion off of a fully-qualified pathname, such as c:\folder\folder\folder\, you would not be able to determine where the filename begins. The POSITION model could tell you where the first "\" is located, but this is of little help. But by reversing the string to be xxx.emanelif\redlof\redlof\redlof\:c, you can find the location of the first "\" and know that everything up to that point is the filename. You would then extract this value and reverse it to get the filename.

The computation requires a repeated dialog called Loop. This dialog has a single temporary number variable in it called LoopLimit-n. The mechanics of the Loop are described in Computation 15: Loops Via REPEAT. The computation also requires a temporary number variable named Temp-n. Both of these temporary variables should have their Advanced options set to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file."

The computation uses the repeated dialog Loop to cycle through each letter of the text variable TextVar in reverse order. Each pass through the loop appends another letter from the string. The reverse order is achieved by subtracting the current COUNTER value from the total number of letters and adding one. For example, the word "mississippi" has a length of 11 characters. The first pass will extract character 11 (length) minus 1 (counter) plus 1, or 11. The second pass will extract character 11 (length) minus 2 (counter) plus 1, or 10. And so on.

bottom of page