Contents / Part 3 / Previous chapter / Next chapter / Index
It has been explained that the BBC computer can store words or other groups of characters in string variables. There are a number of functions which can be used with strings. For example if A$ = "NOTWITHSTANDING" then the string function LEFT$ can be used to copy, say, the left 3 letters of A$ into another string; - B$
10 A$="NOTWITHSTANDING"
20 B$=LEFT$(A$,3)
30 PRINT,B$
>RUN
NOT
Similarly MID$ can be used to extract the middle section of a string. Change line 20 thus
10 A$="NOTWITHSTANDING"
20 B$=MID$(A$,4,9)
30 PRINT B$
>RUN
WITHSTAND
Line 20 can be read as "B$ is a copy of the middle of A$ starting at the fourth letter and going on for 9 letters". As a result of this other flexible use of the word "middle", MID$ can in fact be used to copy any part of a string. Change the program again
10 A$="NOTWITHSTANDING"
20 B$=MID$(A$,1,7)
30 PRINT B$
>RUN
NOTWITH
As well as LEFT$ and MID$ there is the string function RIGHT$ which copies the rightmost characters of a string.
10 A$="NOTWITHSTANDING"
20 B$=RIGHT$(A$,4)
30 PRINT B$
>RUN
DING
It is easy to join two strings together to make a long string by using the "string concatenation operator" which is a plus sign. Its title sounds grand but its purpose is obvious - but quite different from its arithmetic use.
10 A$="NOTWITHSTANDING"
20 B$=LEFT$(A$,3)
30 C$=" LIKELY"
40 D$=B$+C$
50 PRINT D$
>RUN
NOT LIKELY
The numeric function LEN can be used to count up the number of characters in a string - in other words how long it is
10 A$="NOTWITHSTANDING"
20 X=LEN(A$)
30 PRINT X
>RUN
15
LEN is very useful if you don't know how long a string is going to be. For example in this palindrome testing program A$ is copied backwards letter by letter into B$.
5 REPEAT
7 B$=""
10 INPUT "What would you like to reverse? " ' A$
20 FOR T=LEN(A$) TO 1 STEP -1
30 B$=B$ + MID$(A$,T,1)
40 NEXT T
50 PRINT '"If you reverse"' A$'"you get" ' B$
60 UNTIL A$=""
The numeric function on INSTR can be used to see if there is a particular letter (or group of letters) in another string.
10 AS="NOTWITHSTANDING"
20 B$="T"
30 X=INSTR(A$,B$)
50 PRINT X
>RUN
3
You will notice that it finds that there is a T at position 3 in A$. Sometimes it is useful to be able to start the search further along the string. To do this you can add a third parameter which gives that position to start the search.
10 A$="NOTWITHSTANDING"
20 B$="T"
30 X=INSTR(A$,B$,4)
50 PRINT X
>RUN
6
If no match is found then INSTR returns zero.
10 A$="NOTWITHSTANDING"
20 B$="Z"
30 X=INSTR(A$,B$,4)
50 PRINT X
>RUN
0
This looks like the elements of the game called Hangman! But first three more string related functions. It is possible to make a string containing many copies of another string by using the string function STRING$. So to make a string containing 20 copies of "ABC" we write:
10 A$="ABC"
20 B$=STRING$(20,A$)
30 PRINT B$
>RUN
ABCABCABCABCABCABCABCABCABCABCABCABC
ABCABCABCABCABCABCABCABC
There is also a function STR$ which converts a number into a string.
10 A=45:B=30
20 A$=STR$(A)
30 B$=STR$(B)
40 PRINT A+6
50 PRINT A$+B$
>RUN
75
4530
>
i.e. line 40 treats A and B as numbers; and line 50 as string characters.
Note that STR$ is effected by the special variable @% if this has been set (see page 70).
The opposite function is VAL. This extracts the number from the start of the string, which must, start with a plus + or minus - sign or a number. If it doesn't a zero is returned. Numbers which are imbedded in other characters are ignored. So
10 A$="124ABC56"
20 PRINT VAL(A$)
will print 124.
However, back to the outline of a hangman program.
10 MODE 7
20 W=RND(12): REM 12 WORDS TO CHOOSE FROM
30 FOR X= 1 TO W
40 READ A$
50 NEXT X
60 REM WE HAVE SELECTED A RANDOM WORD
70 REM NOW GIVE THE USER CHANCES TO
80 REM GUESS LETTERS IN THE WORD
90 L=LEN(A$)
100 CORRECT=0
110 TRIES=0
120 PRINT TAB(0,5);"The word has ";L;" letters"
130 PRINT TAB(0,6);"You have ";2*L;" tries"
140 REPEAT
150 PRINT TAB(10,7);"GUESS A LETTER";
160 G$=GET$
170 PRINTTAB(25,7);G$
180 P=0
190 REPEAT
200 P=INSTR(A$,G$,P+1)
210 IF P <>0 THEN PRINT TAB(P+12, 15);G$
220 IF P <>0 THEN CORRECT=CORRECT +1
225 IF P=L THEN P=0
230 UNTIL P=0
240 TRIES=TRIES+1
250 PRINT TAB(0,0);"TRIES ";TRIES; TAB(20,0);"CORRECT ";CORRECT
260 UNTIL (CORRECT=L OR TRIES=2*L)
270 IF CORRECT =L THEN PRINT TAB(10,20); "Congratulations"
280 IF TRIES=2*L THEN PRINT TAB(0,16);"The word was ";A$
290 DATA NOTWITHSTANDING
300 DATA INQUISITION
310 DATA MONUMENTAL
320 DATA PRESCRIPTION
330 DATA CARNIVEROUS
340 DATA TENTERHOOK
350 DATA DECOMPRESSION
360 DATA FORTHCOMING
370 DATA NEVERTHELESS
380 DATA POLICEWOMAN.
390 DATA SOPHISTICATED
400 DATA GUESSTIMATE
There are a number of improvements to be made to this program. Its screen layout is poor and also it lets you guess the same letter twice.
It is possible to use some mathematical operators on strings. For example one can check to see if two strings are "equal" or if one string is "greater" than another. Obviously the words "equal" and "greater have slightly unusual meanings when applied to strings. A few examples may help to clarify things. As far as the computer is concerned, "XYZ" is greater than "ABC" because X is further down the alphabet than A. Similarly, "ABC" is greater than "AB" because "ABC" is a longer string.
You can use the following comparisons with strings:
= equal to
<> not equal try
< less than
> greater than
<= less than or equal to
>= greater than or equal.to
The following are legal statements:
IF A$ = "HELLO" THEN PRINT "HOW ARE YOU"
IF B$ >"FIFTEEN" THEN GOTO 1000
Notice that if B$ contained SIX it would be regarded as greater than FIFTEEN because it starts with an S whereas FIFTEEN starts with an F.
10 B$ = "SIX"
20 IF B$ >"EIGHT" THEN STOP.
This program would stop because the word SIX begins with an S which is regarded as "greater than" the letter E.
Strings are compared character by character using ASCII codes.
If two strings start with some sequence of letters, for example PIN and PINT then the longer string is regarded as the larger one.