Handling
Text
Strictly speaking
It
is
Illegal
to
mix data types
in
a LET statement.
It
would be silly
to
write:
LET
num
=
"LION"
and you would get an error message
If
you
tried, but
if
you
write:
LET
num
=
"65
11
•
the
system
will
conclude that you want the number 65
to
become the value of num
and
do
that.
The complete program
is:
100
LET
mark$
=
"625671"
110
LET
hist$
=
mark$(3
TO
4)
120
LET
num =
1.125
*
hist$
130
LET
mark$<3
TO
4)
=num
140
PRINT
mark$
Again the output
is
the same!
In
line
120
a string value
was
converted
Into
numeric form
so
that
it
could be multiplied;
In
line
130
a number was converted
Into
string form.
ThiS
converting of data types
is
known
as
type coercion.
You
can write the program more economically
if
you understand both string-slicing and •
coercion
now:
100
LET
mark$
=
"625671"
110
LET
mark$<3
TO
4)
=
1.125
*
mark$<3
TO
4)
120
PRINT
mark$
If
you
have worked with other BASICs you
will
appreciate the simplicity and power of
string-slicing and coercion.
SEARCHING
A
STRING
OTHER
STRING
FUNCTIONS
You
can search a string for a given substring. The
follOWing
program displays a jumble
of letters and invites
you
to
spot the animal.
100
REM
Animal
Spotting
110
LET
jumbLe$
=
"SYNDICATE"
120
PRINT
jumbLe$
130
INPUT IIWhat
;s
the
animaL?"
!
an$
140
IF
an$
INSTR
jumble$
AND
an$(l)
=
"C"
150
PRINT
"Correct"
150
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.
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$
("*".40l
Finally
it
is
possible
to
use the function CHR$
to
convert internal codes into string
characters. For example:
PRINT
CHR$(65)
would output
A.
12/84
•