Article View: comp.lang.modula2
Article #1280Comparing Strings...
From: Patrick Verkaik
Date: Fri, 19 Oct 1990 04:40
Date: Fri, 19 Oct 1990 04:40
64 lines
2467 bytes
2467 bytes
Hello Bernard,
In a message to All <16 Oct 90 1:06:00> Bernard wrote:
v8> like to be able to find if a string is
v8> equal to, less than, or greater than another string. I tried
v8> writing a routinue
I have seen the following (translated and slightly modified) in a book (by Jim van Keulen):
-+---------------------------------------------------------------
TYPE
CompareType = (Smaller , Greater , Equal);
PROCEDURE CompareString (Str1 , Str2 : ARRAY OF CHAR) : CompareType;
(* CompareString returns
* Smaller if Str1 < Str2,
* Equal if Str1 = Str2,
* Greater if Str1 > Str2 *)
VAR
i : CARDINAL;
LengthStr1 , LengthStr2 , LengthShortest : CARDINAL;
BEGIN
LengthStr1 := LengthString (Str1);
LengthStr2 := LengthString (Str2);
IF LengthStr1 <= LengthStr2
THEN
LengthShortest := LengthStr1
ELSE
LengthShortest := LengthStr2
END;
FOR i := 0 TO LengthShortest DO
IF Str1 [i] < Str2 [i]
THEN
RETURN Smaller
ELSIF Str1 [i] > Str2 [i]
RETURN Greater
END
END;
(* One of either strings forms the beginning of the other *)
IF LengthShortest < LengthStr2
THEN
RETURN Smaller
ELSIF LengthShortest < LengthStr1
RETURN Greater
ELSE
RETURN Equal
END
END CompareString;
-+---------------------------------------------------------------
That's it, I've assumed you do have a procedure that returns the length of a string (LengthString or whatever yours is called) otherwise I can give you that source too, if you want it...
Hope this has helped you!
PDV
--
uucp: uunet!m2xenix!puddle!2!512!124.99!Patrick.Verkaik
Internet: Patrick.Verkaik@p99.f124.n512.z2.fidonet.org
Technical Details
Message-ID:
<339.27238882@puddle.fidonet.org>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!seismo!dimacs.rutgers.edu!rutgers!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!dali.cs.montana.edu!ogicse!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!p99.f124.n512.z2.fidonet.org!Patrick.VerkaikShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!seismo!dimacs.rutgers.edu!rutgers!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!dali.cs.montana.edu!ogicse!zephyr.ens.tek.com!tektronix!nosun!qiclab!m2xenix!puddle!p99.f124.n512.z2.fidonet.org!Patrick.Verkaik From: Patrick.Verkaik@p99.f124.n512.z2.fidonet.org (Patrick Verkaik) Newsgroups: comp.lang.modula2 Subject: Comparing Strings... Message-ID: <339.27238882@puddle.fidonet.org> Date: 19 Oct 90 04:40:45 GMT Sender: ufgate@puddle.fidonet.org (newsout1.26) Organization: FidoNet node 2:512/124.99 - The Owl's Nest Node, Soest Lines: 64