Chapter 21.2 - Substrings |
Exercises1. Some BASICs (not the ZX81 BASIC) have functions called LEFT$, RIGHT$, MID$ & TL$.LEFT$(A$,N) gives the substring of A$ consisting of the first N characters. RIGHT$(A$,N) gives the substring of A$ consisting of the characters from the Nth on. MID$(A$,N1,N2) gives the substring of A$ consisting of N2 characters starting at the N1th. TL$(A$) gives the substring of A$ consisting of all its characters except the first. How would you write these in ZX81 BASIC? Would your answers work with strings of length 0 or 1? 2. Try this sequence of commands: LET A$="X*+*Y" LET A$(2)=CHR$ 11 [the string quote character] LET A$(4)=CHR$ 11 PRINT A$ A$ is now a string with string quotes inside it! So there is nothing to stop you doing this if you are persevering enough, but clearly if you had originally typed LET A$="X"+"Y" the part to the right of the equals sign would have been treated as an expression, giving A$ the value "XY". Now type LET B$="X""+""Y" You will find that although A$ & B$ look the same when printed out, they are not equal - try PRINT A$=B$ Whereas B$ contains mere quote image characters (with code 192), A$ contains genuine string quote characters (with code 11). 3. Run this program: 10 LET A$="LEN ""ABDC""" 100 PRINT A$;" = ";VAL A$ This will fail because VAL does not treat the quote image "" as a string quote. Insert some extra lines between 10 & 100 to replace the quote images in A$ by string quotes (which you must call CHR$ 11), & try again. Make the same modifications to the program in chapter 9, exercise 3, & experiment with it. 4. This subroutine deletes every occurrence of the string "CARTHAGO" from A$. 1000 FOR N=1 TO LEN A$-7 1020 IF A$(N TO N+7)="CARTHAGO" THEN LET A$(N TO N+7)="********" 1030 NEXT N 1040 RETURN Write a program that gives A$ various values (e.g. "DELENDA EST CARTHAGO.") & applies the subroutine. |