A string is a character or a group 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. &nb sp;This is useful because it allows one to write "friendly" programs, where individuals can be referred to by their names instead of a number.
DS5000-BASIC 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 DS5000-BASIC. Initial ly, NO memory is allocated for strings. Memory is allocated by the STRING [expr1], [expr2] STATEMENT. expr1 is the total number of bytes allocated to strings plus 1 times the number of strings plus 1, expr2 is the number of bytes in each string. &nb sp; STRING 14, 12 would allocate 12 characters to the first string $(0). The details of this statement are covered in the DESCRIPTION OF STATEMENTS chapter of this manual.
In DS5000-BASIC, strings can be defined in two ways, with the LET STATEMENT and with the INPUT STATEMENT.
EXAMPLE:
WHAT'S YOUR NAME? - FRED
>10 STRING 100,20
>20 $(l)="THIS IS A STRING, "
>30 INPUT "WHAT'S YOUR NAME? - ",$(2)
>40 PRINT $(l),$(2)
>RUN
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).
In DS5000-BASIC, two operators manipulate STRINGS. These operators are ASC( ) and CHR( ). Admittedly, the string operators contained in DS5000-BASIC are not quite as powerful as the string operators contained in some BASICS. But surprisi ngly enough, by using the string operators available in DS5000-BASIC it is possible to manipulate strings in almost any way imaginable. This in itself is a commendable feat since DS5000-BASIC was designed primarily to be a sophisticated BASIC langua ge oriented controller, not a string manipulator.
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 predefined ASCII string can be evaluated with the ASC( ) operator.
EXAMPLE:
THIS IS A STRING
>5 STRING 100,20
>10 $(1)="THIS IS A STRING"
>20 PRINT $(1)
>30 PRINT ASC($(1),1)
>RUN
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 st ring was picked out and 84 is the decimal representation for the ASCII character "T".
EXAMPLE:
65 66 67 68 69 70 71 72 73 74 75 76
>5 STRING 100,15
>10 $(1)="ABCDEFGHIJKL"
>20 FOR X= 1 TO 12
>30 PRINT ASC($(1),X),
>40 NEXT X
>RUN
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:
ABCDEFGHIJKL
>5 STRING 100,15
>10 $(l)="ABCDEFGHIJKL"
>20 PRINT $(1)
>30 ASC($(1),1)=75
>40 PRINT $(1)
>50 ASC($(1),2)=ASC($(1),3)
>60 PRINT $(1)
>RUN
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:
WHAT'S THE PASSWORD - SECURE
>5 STRING 100,10
>10 $(1)="SECRET" : REM SECRET IS THE PASSWORD
>20 INPUT "WHAT'S THE PASSWORD - ",$(2)
>30 FOR T=1 TO 6
>40 IF ASC($(1),T)=ASC($(2),T) THEN NEXT T ELSE 70
>50 PRINT "YOU GUESSED IT"
>60 END
>70 PRINT "WRONG, TRY AGAIN" : GOTO 20
WRONG, TRY AGAIN
WHAT'S THE PASSWORD - SECRET
YOU GUESSED IT
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:
DS5000-BASIC
>5 STRING 14,12
>10 $(0)="DS5000-BASIC"
>20 FOR T=1 TO 12 : PRINT CHR($(0),t), : NEXT T
>30 PRINT : FOR T=12 TO 1 STEP -1
>40 PRINT CHR($(0),T), : NEXT T
>RUN
CISAB-0005SD
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($(I),I) = 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!