Log"
COMPARING
STRINGS
FOR SERVICE MANUALS
CONTACT:
MAURITRON
TECHNICAL
SERVICES
www,mauritron,co,uk
TEL:
01844·
3~1694
FAX:
01844
- 352554
IF "CAT" <
"DOG"
THEN
PRINT
"MEOW"
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 fllst
Because the letters
AB,C
...
are internally
coded
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:
and the output would be:
MEOW
Similarly:
IF
"DOG"
>
"CAT"
THEN
PRINT
"WOOF"
would give the output:
WOOF
We
use the comparison symbols
of
mathematics for string comparisons,
All
the following
logical statements expressions are both permissible and
true,
"ALF" < "BEN"
IlK!T"
>
"BEN"
"KIT" <=
"LEN"
IIK!T"
>=
"KIT"
"PAT"
>=
"LEN"
"LEN"
<:
"LEN"
"PAT"
<>
"PET"
So
far,
comparisons based simply on internal codes make sense, but data
is
not always
conveniently restricted to upper case letters.
We
would
like,
for example:
Cat to be less than COT
and K2N to be less than
K27.N
A simple character by character comparison based on internal codes would not give
these results,
so
SuperBASIC behaves
in
a more intelligent
way,
The following program,
with suggested input and the output that
will
result, illustrates the rules for comparison
of strings,
100
REMark
comparisons
110
REPeat
camp
120
INPUT
"input
a
string"
!
fi
rst$
130
INPUT
"input
another
string"
!
secondS
140
IF
fi
rst$
<
secondS
THEN
PRINT
"Less"
150
IF
fi
rst$
>
secondS
THEN
PRINT
"Greater"
160
IF
first$
=
secondS
THEN
PRINT
"EquaL"
17.0
END
REPeat
camp
input
output
CAT
car Greater
CAT
CAT
Equal
PET
PETE
Less
K6
K7.
Less
K66
K7.
Greater
K12N
K6N Greater