home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / strings.swg / 0107_String Arrays.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-05-26  |  2.6 KB  |  102 lines

  1.  
  2. { Updated STRINGS.SWG on May 26, 1995 }
  3.  
  4. {
  5.  OK, this is the working version of the old one.
  6.  I tested it and it worked.
  7.  
  8.  Insert_Arr2Strs procedure, and a little demo on how to use it
  9.  
  10.  by Christopher J. Chandra - 1/25/95
  11.  
  12.  PUBLIC DOMAIN CODE
  13. }
  14.  
  15. uses crt;
  16.  
  17. type str_array=array[1..128] of char;
  18.      str127=string[127];
  19.  
  20. procedure insert_arr2strs(s:str_array;var r1,r2:str127);
  21. var cnt,cnt2,eidx:integer;
  22.  
  23. begin
  24.  cnt:=1;
  25.  eidx:=length(r1);
  26.  r2:='';
  27.  
  28.  {assuming that the array is a NULL terminated string...}
  29.  
  30.  while ((s[cnt]<>#0) and (cnt<128) and (eidx+cnt<128)) do
  31.  begin
  32.   r1[eidx+cnt]:=s[cnt];    {copy the array into the 1st result string}
  33.   inc(cnt);
  34.  end;
  35.   r1[0]:=chr(eidx+cnt-1);  {store the string length}
  36.  
  37.  {if any left over, do ...}
  38.  
  39.  cnt2:=1;
  40.  
  41.  while ((s[cnt]<>#0) and (cnt<129)) do
  42.  begin
  43.   r2[cnt2]:=s[cnt];        {copy the left over into the 2nd result string}
  44.    inc(cnt);
  45.    inc(cnt2);
  46.  end;
  47.   r2[0]:=chr(cnt2-1);      {store the string length}
  48.  
  49. end;
  50.  
  51. var myarray:str_array;
  52.     mystr1,mystr2:str127;
  53.     cnt:integer;
  54.     s:string;
  55.  
  56. begin
  57.  clrscr;
  58.  
  59.  s:='Ain''t that a nice song?  OK, here is another one ... ';
  60.  for cnt:=1 to length(s) do myarray[cnt]:=s[cnt];myarray[cnt+1]:=#0;
  61.  
  62.  mystr1:='London Bridge is falling down, falling'+
  63.          ' down, falling down.  London Bridge is'+
  64.          ' falling down, my fair lady. WHOOSH!  ';
  65.  mystr2:='';
  66.  
  67.  textcolor(12);writeln('Before insertation ...');
  68.  textcolor(10);write('String 1:');
  69.  textcolor(14);writeln('"',mystr1,'"');
  70.  textcolor(10);write('String 2:');
  71.  textcolor(14);writeln('"',mystr2,'"');writeln;
  72.  textcolor(11);write('String Array to be inserted:');
  73.  textcolor(13);writeln('"',s,'"');writeln;
  74.  
  75.  insert_arr2strs(myarray,mystr1,mystr2);
  76.  
  77.  textcolor(12);writeln('After insertation ... using String 2 for leftovers');
  78.  textcolor(10);write('String 1:');
  79.  textcolor(14);writeln('"',mystr1,'"');
  80.  textcolor(10);write('String 2:');
  81.  textcolor(14);writeln('"',mystr2,'"');writeln;
  82.  
  83.  s:='One Little Two Little Three Little Indians.  '+
  84.     'Four Little Five Little Six Little Indians.  '+
  85.     'Seven Little Eight Little ';
  86.  for cnt:=1 to length(s) do myarray[cnt]:=s[cnt];myarray[cnt+1]:=#0;
  87.  
  88.  textcolor(11);write('String Array to be inserted:');
  89.  textcolor(13);writeln('"',s,'"');writeln;
  90.  
  91.  insert_arr2strs(myarray,mystr2,mystr1);
  92.  
  93.  textcolor(12);writeln('After insertation ... using String 1 for leftovers');
  94.  textcolor(10);write('String 1:');
  95.  textcolor(14);writeln('"',mystr1,'"');
  96.  textcolor(10);write('String 2:');
  97.  textcolor(14);writeln('"',mystr2,'"');writeln;
  98.  
  99.  textcolor(12);writeln('End of demo.  :)');
  100.  
  101. end.
  102.