Unit 8.2 -- Compare Two Character Strings
            SILVER Section 3.2 specificially
            SCHAUM Section 7.4

PROG

Compare two character strings and return
  -1  if first string < second string
   0  if first string = second string
  +1  if first string > second string

PED

To understand the concept of lexical comparison of character
strings
To see another character string example

CONCEPTS

string1 < string2 if string1 would appear before string2  in
a dictionary

SF

CLM regA,1,memory-addr

-compares  rightmost byte of "regA" with  byte  at  "memory-
addr"

(same condition codes as C and CR)

32.html

program unit35(input,output);
type charstring=record
                  MaximumLength:integer;
                  ActualLength:integer;
                  Chars:array[1..100] of char;
    {remember that our mechanism allows character strings of
various sizes}
                end;
var string1,string2:charstring;
    answer:integer;

{this is not translated}
procedure readstring(var s:charstring);
var i:integer;
begin
i:=1;
while (i<=s.MaximumLength) and (not eoln(input)) do begin
  read(s.Chars[i]);
  i:=i+1;
end;
i:=i-1;
readln(input);
s.ActualLength:=i;
end;

{nor is this routine}
procedure writestring (s:charstring);
var i:integer;
begin
for i:=1 to s.ActualLength do begin
  write(s.Chars[i]);
end;
writeln;
end;

{this is the key routine}
{compare two character strings}
{returns
   -1 first string < second string
    0 first string = second string
    1 first string > second string
}
procedure             compare(string1,string2:charstring;var
result:integer);
var i:integer;
begin
result:=0; {items are equal until proven otherwise}
i:=1;
while(result=0)and(i<=string1.ActualLength)and(i<=string2.Ac
tualLength) do begin
  if string1.Chars[i]<string2.Chars[i] then begin
    result:=-1;
  end
  else
  if string1.Chars[i]>string2.Chars[i] then begin
    result:=+1;
  end;
  i:=i+1;
end;
if result=0 then begin
  if string1.ActualLength<string2.ActualLength then begin
    result:=-1;
  end
  else
  if string1.ActualLength>string2.ActualLength then begin
    result:=+1;
  end;
end;
end;


begin
write('please  give  me  two strings  to  compare,  one  per
line');
string1.MaximumLength:=100;
string2.MaximumLength:=100;
readstring(string1);
readstring(string2);
write('string1 is ');writestring(string1);
write('string2 is ');writestring(string2);
compare(string1,string2,answer);
write('the answer is ',answer);writeln;
end.