{$m-}
 
const	stringmax=132;
 
type	string = record
	   size: integer;
	   len: integer;
	   ch: array[1..stringmax] of char
	   end;
 
	outcometyp = ( SEARCHING, THINGABSENT, THINGFOUND );
 
 
function length ( var s: string ): integer;
var  i: integer;
     outcome: outcometyp;
begin
i := s.size;
outcome := SEARCHING;
repeat
    if s.ch[i] <> ' ' then outcome := THINGFOUND
    else
	begin
	i := pred(i);
	if i <= 0 then outcome := THINGABSENT
	end
    until outcome <> SEARCHING ;
length := i
end;
 
procedure clear(var s:string);
var	i: integer;
begin	s.len:=0;
	for i:=1 to s.size do s.ch[i]:=' '
end;
 
procedure concatenate(var s:string; var t:string);
var	i,j: integer;
begin
	if s.len+t.len > s.size
	   then j := s.size-s.len { overflow }
	   else j := t.len;
	for i:=1 to j do  s.ch[s.len+i] := t.ch[i];
	s.len := s.len+j;
end;
 
function search ( var s,t:string; start:integer):integer;
var	i,j: integer;
	uneq: boolean;
begin
	if start<1 then start := 1;
	if (start+t.len>s.len+1) or (t.len=0)
	   then search := 0
	   else
	      begin
	      i := pred(start);
	      repeat
		 i := succ(i);  j := 0;
		 repeat
		    j := succ(j);
		    uneq := t.ch[j]<>s.ch[i+j-1];
		 until uneq or (j=t.len);
	      until (not uneq) or (i=s.len-t.len+1);
	      if uneq
		 then search := 0
		 else search := i;
	      end;
end;
 
 
procedure writestring (var f:text; var s:string);
var	i: integer;
begin	for i:=1 to s.len do write(f,s.ch[i])
end;
 
 
procedure delete(var s:string; start,span:integer);
var	i,limit: integer;
begin
	if span<0 then begin span := -span;  start := start-span end;
	limit := start+span;
	if start<1 then start := 1;
	if limit>succ(s.len) then limit:=succ(s.len);
	span := limit-start;
	if span>0 then
	   begin
	   for i:=0 to s.len-limit do s.ch[start+i] := s.ch[limit+i];
	   for i:=s.len-span+1 to s.len do s.ch[i] := ' ';
	   s.len := s.len-span;
	   end;
end;
 
procedure insert (var s:string; var t:string; p:integer);
var	i,j: integer;
begin
	if t.len>0 then
	   if (p>0) and (p<=succ(s.len))
	      then
		 begin
		 if s.len+t.len<=s.size
		    then s.len := s.len+t.len
		    else s.len:=s.size { overflow } ;
		 for i:=s.len downto p+t.len do s.ch[i]:=s.ch[i-t.len];
		 if s.len<p+t.len
		    then j:=s.len
		    else j:=p+t.len-1;
		 for i:=p to j do s.ch[i]:=t.ch[i-p+1];
		 end
	      { else error: non-contiguous string }
end.
