130 INPUT "What is the animal?" ! an$
140 IF an$ INSTR jumble$ AND an$(1) = "C"
150 PRINT "Correct"
160 ELSE
170 PRINT "Not correct"
180 END IF
The operator INSTR, returns zero if the guess is incorrect. If the guess is correct INSTR returns the
number which is the starting position of the string-slice, in this case 6.
Because the expression:
an$ INSTR jumble$
can be treated as a logical expression the position of the string in a successful search can
be regarded as true, while in an unsuccessful search it can be regarded as false.
OTHER STRING FUNCTIONS
You have already met LEN which returns the length (number of characters) of a string. You may wish
to repeat a particular string or character several times. For example, if you wish to output a row of
asterisks, rather than actually enter forty asterisks in a PRINT statement or organise a loop you can
simply write:
PRINT FILL$ ("*",40)
Finally it is possible to use the function CHR$ to convert internal codes into string characters. For
example:
PRINT CHR$(65)
would output A.
COMPARING STRINGS
A great deal of computing is concerned with organising data so that it can be searched quickly.
Sometimes it is necessary to sort it in to alphabetical order. The basis of various sorting processes is
the facility for comparing two strings to see which comes first. Because the letters A,B,C ... are
internally oded as 65,66,67 .... it is natural to regard as correct the following statements:
A is less than B
B is less than C
and because internal character by character comparison is automatically provided:
CAT is less than DOG
CAN is less than CAT
You can write, for example:
IF "CAT" < "DOG" THEN PRINT "MEOW"
and the output would be:
MEOW
Similarly:
IF "DOG" > "CAT" THEN PRINT "WOOF"