## CHAPTER 6

### Description of String Operators

6.1 WHAT ARE STRINGS?

A string is a character or a bunch of characters that are stored in memory. Usually, the characters stored in a string make up a word or a sentence. Strings are handy because they allow the programmer to deal with words instead of numbers. This is useful because it allows one to write "friendly" programs, where individuals can be referred to by their names instead of a number.

MCS BASIC-52 contains one dimensioned string variable, $([expr]). The dimension of the string variable (the [expr] value) ranges from 0 to 254. This means that 255 different strings can be defined and manipulated in MCS BASIC-52. Initially, no memory is allocated for strings. Memory is allocated by the STRING [expr], [expr] statement.

In MCS BASIC-52, strings can be defined in two ways, with the LET statement and with the INPUT statement.

EXAMPLE:

    >10 STRING 100,20
    >20 $(1)="THIS IS A STRING, "
    >30 INPUT "WHAT'S YOUR NAME? - ",$(2)
    >40 PRINT $(1),$(2)
    >RUN

    WHAT'S YOUR NAME?. - FRED

    THIS IS A STRING, FRED

Strings can also be assigned to each other with a LET statement.

EXAMPLE:

            $(2)=$(1)

Would assign the string value in $(1) to the string $(2).

6.2 THE ASC OPERATOR

In MCS BASIC-52, two operators manipulate strings. These operators are ASC( ) and CHR( ). Admittedly, the string operators contained in MCS BASIC-52 are not quite as powerful as the string operators contained in some BASICs. But surprisingly enough, by using the string operators available in MCS BASIC-52 it is possible to manipulate strings in almost any way imaginable. This in itself is a commendable feat since MCS BASIC-52 was designed primarily to be a sophisticated BASIC language oriented controller, not a string manipulator. The string operators available in MCS BASIC-52 are as follows:

ASC( )

The ASC( ) operator returns the integer value of the ASCII character placed in the parentheses.

EXAMPLE:

    >PRINT ASC(A)
    65

65 is the decimal representation for the ASCII character "A" In addition, individual characters in a pre-defined ASCII string can be evaluated with the ASC( ) operator.

EXAMPLE:

    >10 $(1)="THIS IS A STRING"
    >20 PRINT $(1)
    >30 PRINT ASC($(1),1)

    THIS IS A STRING
     84

When the ASC( ) operator is used in the manner shown above, the $([expr]) denotes what string is being accessed and the expression after the comma "picks out" an individual character in the string. In the above example, the first character in the string was picked out and 84 is the decimal representation for the ASCII character " T "

EXAMPLE:

    >10 $(1)="ABCDEFGHIJKL"
    >20 FOR X=1 TO 12
    >30 PRINT ASC($(1),X),
    >40 NEXT X
    >RUN

     65 66 67 68 69 70 71 72 73 74 75 76

The numbers printed in the previous example are the values that represent the ASCII characters A,B,C,. . . L.

Additionally, the ASC( ) operator can be used to change individual characters in a defined string.

EXAMPLE:

    >10 $(1)="AECDEFGHIJKL"
    >20 PRINT $(1)
    >30 ASC($(1),1)=75
    >40 PRINT $(1)
    >50 ASC($(1),2)=ASC($(1),3)
    >60 PRINT $(1)
    >RUN

    AECDEFGHIJKL
    KBCDEFGHIJKL
    KCCDEFGHIJKL

In general, the ASC( ) operator lets the programmer manipulate individual characters in a string. A simple program can determine if two strings are identical.

EXAMPLE:

    >10 $(1)="SECRET" : REM SECRET IS THE PASSWORD
    >20 INPUT "WHAT'S THE PASSWORD - ",$(2)
    >30 FOR I=l TO 6
    >40 IF ASC($(l),I)=ASC($(2),I) THEN NEXT I ELSE 70
    >50 PRINT "YOU GUESSED IT"'
    >60 END
    >70 PRINT "WRONG. TRY AGAIN" : GOTO 20
    >RUN

    WHAT'S THE PASSWORD - SECURE
    WRONG, TRY AGAIN
    WHAT'S THE PASSWORD - SECRET
    YOU GUESSED IT

6.3 THE CHR OPERATOR

CHR( )

The CHR( ) operator is the converse of the ASC( ) operator. It converts a numeric expression to an ASCII character.

EXAMPLE:

    >PRINT CHR(65)
    A

Like the ASC( ) operator, the CHR( ) operator can also "pick out" individual characters in a defined ASCII string.

EXAMPLE:

    >10 $(1)"MCS BASIC-52"
    >20 FOR I=1 TO 12 : PRINT CHR($(1),I), : NEXT I
    >30 PRINT : FOR I=12 TO 1 STEP -1
    >40 PRINT CHR($(1),I), : NEXT I
    >RUN

    MCS BASIC-52
    25-CISAB SCM

In the above example, the expressions contained within the parentheses, following the CHR operator have the same meaning as the expressions in the ASC( ) operator.

Unlike the ASC( ) operator, the CHR( ) operator cannot be assigned a value. A statement such as CHR($(1),1) = H, is invalid and will generate a BAD SYNTAX error. Use the ASC( ) operator to change a value in a string. The CHR( ) operator can only be used within a print statement!