compare

Write here if you have problems with your Pascal source code

Moderator: Board moderators

Post Reply
ACoimbra
New poster
Posts: 14
Joined: Thu Apr 10, 2003 1:59 pm
Location: Coimbra, Portugal
Contact:

compare

Post by ACoimbra »

Could anyone write an efficient compare function to compare two strings plz
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post 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]
ACoimbra
New poster
Posts: 14
Joined: Thu Apr 10, 2003 1:59 pm
Location: Coimbra, Portugal
Contact:

like strings

Post 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?
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post 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...
ACoimbra
New poster
Posts: 14
Joined: Thu Apr 10, 2003 1:59 pm
Location: Coimbra, Portugal
Contact:

thnx

Post by ACoimbra »

Thnx for your help :D
Post Reply

Return to “Pascal”