Page 1 of 1

compare

Posted: Thu Apr 10, 2003 2:49 pm
by ACoimbra
Could anyone write an efficient compare function to compare two strings plz

Posted: Thu Apr 10, 2003 8:08 pm
by little joey
Try this:
[pascal]program test;

function scompare(var s1,s2:ansistring):integer;
var
l1,l2,minlen,i:integer;
begin
l1:=length(s1);
l2:=length(s2);
if (l1<l2) then minlen:=l1 else minlen:=l2;
i:=1;
while ((i<=minlen) and (s1=s2)) do inc(i);
if (i>minlen) then scompare:=l1-l2 else scompare:=ord(s1)-ord(s2);
end;

var
s1,s2:ansistring;
c:integer;
begin
s1:='smallest';
s2:='biggest';
c:=scompare(s1,s2);
if (c<0) then writeln('"',s1,'" is smaller than "',s2,'"')
else if (c=0) then writeln('"',s1,'" and "',s2,'" are equal')
else writeln('"',s1,'" is bigger than "',s2,'"');
end.[/pascal]

like strings

Posted: Fri May 09, 2003 1:53 pm
by ACoimbra
Well, I heard that I can use something like string1>string2, but I'm not sure, in the acm.uva.es/p part of pascal known problems, they say that in gpc using unix the > would fail, but with freepascal there is any problem?

Posted: Sat May 10, 2003 10:45 am
by little joey
That is fixed. You can use all comparison operators on strings now.
The UVA-pages on Pascal are quite out of date...

thnx

Posted: Sat May 10, 2003 7:58 pm
by ACoimbra
Thnx for your help :D