Return to Statement


Compare Strings Example

This example demonstrates how using the CMPSTR function can replace many lines of programming. The object of the example is to return value (-1, 0, or 1) to the variable ANS which is used later in the program.

Using the CMPSTR function as shown below,

   ANS=CMPSTR('ABZ','ABC')

is equivalent to the following expressions:

   DO/A1:,I,1,3
NUM1=ASCII('ABZ',I)
NUM2=ASCII('ABC',I)
IF/(NUM1-NUM2),LO:,,HI:
A1:
JUMP/A2:
LO:
ANS=-1
JUMP/A2:
HI:
ANS=1
A2:

NOTE: The first unequal position is position three and the character Z as shown in the ASCII Conversion Table lexically follows the character C, therefore, the value of ANS is 1.

Several more strings are compared below:

Function

Assigned Value

ANS=CMPSTR('D2','D19')

ANS=1

ANS=CMPSTR('D9','DD')

ANS=-1