[comp.lang.pascal] Cool strings unit

wct@po.CWRU.Edu (William C. Thompson) (04/17/91)

Here's a cool unit to do stuff with strings...


unit strings;
interface

function allup(s:string):string;
{ changes all letters in a string to uppercase }

function lowcase(c:char):char;
{ un-captializes a letter }

function alllow(s:string):string;
{ un-capitalizes a string }

function rep(s:string; n:integer):string;
{ makes n copies of s }

procedure reverse(var s:string);
{ reverses s }

implementation

function allup(s:string):string;
var i:byte;
begin
  for i:=1 to length(s) do s[i]:=upcase(s[i]);
  allup:=s
end;

function lowcase(c:char):char;
begin
  if c in ['A'..'Z'] then c:=chr(ord(c)+32);
  lowcase:=c
end;

function alllow(s:string):string;
var i:byte;
begin
  for i:=1 to length(s) do s[i]:=lowcase(s[i]);
  alllow:=s
end;

function rep(s:string; n:integer):string;
var
  t: string;
  i: integer;
begin
  t:='';
  for i:=1 to n do t:=t+s;
  rep:=t
end;

procedure reverse(var s:string);
var
  t: string;
  l,i: integer;
begin
  l:=length(s);
  t:=s;
  for i:=l downto 1 do t[l+1-i]:=s[i];
  s:=t
end;

end.
-- 
Ticking away, the moments that make up a dull day.   | William C. Thompson
You fritter and waste the hours in an offhand way.   | Michelson 620D (wct)
Kicking around on a piece of ground in your hometown,| a.k.a. Master of Time,
waiting for someone or something to show you the way.| Space, and Dimension

sdalelt@cc.curtin.edu.au (04/20/91)

In a msg dated [Wed Apr 17 1991], William C. Thompson wrote:

 > WCT: Here's a cool unit to do stuff with strings...

 > WCT: unit strings;
 > WCT: interface
 > WCT: [..]

You may want to add the following to your strings unit.  Since it uses some
non-standard stuff, it only works with Turbo Pascal.

  {---------------------------------------------------------------------------}

  function pretty(s : string) : string;
   {-prettify a string to correct case                                        }

  var
    count                    : byte;
    sl                       : byte absolute s;
    flag                     : boolean;

    {-------------------------------------------------------------------------}

    function lowcase(c : char) : char;

    var
      b                      : byte absolute c;

    begin
      c := upcase(c);
      if c in ['A'..'Z'] then b := b + 32;

      lowcase := c;
    end;

    {-------------------------------------------------------------------------}

  begin
    flag := false;

    for count := 1 to sl do
      if upcase(s[count]) in ['A'..'Z'] then
        if flag then s[count] := lowcase(s[count])
                else
                  begin
                    flag := true;
                    s[count] := upcase(s[count]);
                  end
        else flag := false;

    pretty := s;
  end;

 {---------------------------------------------------------------------------}

Cheers,
Lincoln.
_____________________________________________________________________________
 Internet: sdalelt@cc.curtin.edu.au                    Voice: +61-9-459-4701
           lincoln_dale@f627.n690.z3.fidonet.org
 FidoNet:  Lincoln Dale @ 3:690/627.0                   Data: +61-9-493-1534
 PSImail:  psi%050529452300070::sdalelt                       +61-9-493-2625
 UUCP:     uunet!munnari.oz!cc.curtin.edu.au!sdalelt
 Bitnet:   sdalelt%cc.curtin.edu.au@cunyvm.bitnet