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 » Thu Apr 10, 2003 2:49 pm

Could anyone write an efficient compare function to compare two strings plz

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Thu Apr 10, 2003 8:08 pm

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 » Fri May 09, 2003 1:53 pm

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?

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Sat May 10, 2003 10:45 am

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 » Sat May 10, 2003 7:58 pm

Thnx for your help :D

Post Reply

Return to “Pascal”