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. The details of this statement are covered in the DESCRIPTION OF STATE- MENTS chapter of this manual.
In MCS BASIC-52, strings can be defined in two ways, with the LET STATEMENT and with the INPUT STATEMENT.
>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.
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( ). Ad- mittedly, 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:
The ASC( ) operator returns the integer value of the ASCII character placed in the parentheses.
>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.
>10 $(1)="THIS IS A STRING" >20 PRINT $(1) >30 PRINT ASC($(1),1) THIS IS A STRING
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 ."
6.2 THE ASC OPERATOR
>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.
>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.
>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
The CHR( ) operator is the converse of the ASC( ) operator. It converts a numeric expression to an ASCII character.
>PRINT CHR(65) A
Like the ASC( ) operator, the CHR( ) operator can also "pick out" individual characters in a defined ASCII string.
>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 3ASIC-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!
Copyright © Madis Kaal 2000-