Recent

Author Topic: find word in string  (Read 18007 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: find word in string
« Reply #15 on: January 16, 2019, 12:39:09 pm »
You should use or at least give Lazarus a try.
Lazarus is an IDE for FreePascal, it allow you to create GUI and non-GUI programs. It has many awesome features to help you do create easily.

http://wiki.lazarus.freepascal.org/New_IDE_features_since
http://wiki.freepascal.org/Lazarus_IDE_Shortcuts
http://wiki.freepascal.org/IDE_tricks

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: find word in string
« Reply #16 on: January 16, 2019, 12:54:02 pm »
Hi

StrUtils has many useful routines,FindMatchesBoyerMooreCaseSensitive.

Code: Pascal  [Select][+][-]
  1.  
  2. Uses ........ ,strutils;
  3.  
  4. var match_location:sizeintarray;
  5.     s:string='';
  6.     i:longint;
  7.     Look_In_String:String='abababababab';
  8.     Find_String:String='aba';
  9. begin
  10.   S:='Finding :'+Find_String+' In :'+Look_In_String+slinebreak;
  11.   FindMatchesBoyerMooreCaseSensitive(Look_In_String,Find_String,match_location,true);
  12.   If High(Match_Location)<>Low(Match_Location) then
  13.   begin
  14.     s:=s+'Matches :'+inttostr(High(match_location)+1)+slinebreak;
  15.     for i:=Low(Match_Location) to High(Match_Location) do s:=s+'Loc :'+inttostr(match_location[i])+slinebreak;
  16.   end
  17.   else s:='No Matches';
  18.   WriteLn(s);
  19. end;                  
  20. [/pascal]
« Last Edit: January 16, 2019, 06:22:51 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: find word in string
« Reply #17 on: January 16, 2019, 07:21:53 pm »

Thank you, I'll try the ide sometime.
To agree with the sysutils method I have altered my previous tally routine although I have cranked up the goto's in the process.
Code: Pascal  [Select][+][-]
  1.  
  2. program tally;
  3.  
  4. Type  
  5.   intArray = Array of integer;
  6.  
  7. // =========  number of partstring in somestring =============//
  8.  function tally(somestring:pchar;partstring:pchar;out arr: intarray ):integer;
  9. var
  10. i,j,ln,lnp,count,num:integer ;
  11. filler:boolean;
  12. label
  13. skip ,start,return;
  14. begin
  15.  ln:=length(somestring);
  16. lnp:=length(partstring);
  17. filler:=false;
  18. start:
  19. count:=0;
  20. i:=-1;
  21. repeat
  22. i+=1;
  23.    if somestring[i] <> partstring[0] then goto skip ;
  24.      if somestring[i] = partstring[0] then
  25.      begin
  26.      for j:=0 to lnp-1 do
  27.      begin
  28.      if somestring[j+i]<>partstring[j] then goto skip;
  29.      end;
  30.       count+=1;
  31.       if filler = true then arr[count]:=i+1 ;
  32.       i:=i+lnp-1;
  33.      end ;
  34.    skip:
  35.    until i>=ln-1 ;
  36. SetLength(arr,count); // size is now known, repeat the operation to fil arr
  37. arr[0]:=count;        // save tally in [0]
  38. num:=count;
  39. if filler=true then goto return;
  40. filler:=true;
  41.   goto start;
  42.    return:
  43.   result:=num;
  44. end; {tally}
  45.  
  46.     //=========== Trial =========== //
  47.  
  48.  var
  49.  arr:intarray;
  50.  p:pchar;
  51.  s:ansistring;
  52.  i,num:integer;
  53.  comma:string;
  54.  begin
  55.  s:='abababababab';
  56.  for i:=1 to 5 do
  57.  begin
  58.  s+=s;
  59.  end;
  60.  
  61.  p:=pchar(s);        // cast
  62.  writeln(s);
  63.  num:=tally(p,'aba',arr);
  64.  writeln('Tally of aba  ',num);
  65.  
  66.  write('string length  =  ');
  67.  writeln(length(p));
  68.  
  69.  writeln('Positions:');
  70.   for i:=1 to arr[0] do
  71.   begin
  72.   if i<arr[0] then comma:=',' else comma:='';
  73.  write(arr[i],comma);
  74.  end;
  75.  writeln;
  76.  writeln('Press enter to end');
  77.  
  78.    readln;
  79.  end.
  80.  
  81.      

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: find word in string
« Reply #18 on: January 16, 2019, 08:57:28 pm »
Line 23 is unnecessary.
Replace goto start with
Code: Pascal  [Select][+][-]
  1.  for filler:=false to true
and gotos are gone.

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: find word in string
« Reply #19 on: January 17, 2019, 03:17:13 am »
Code: Pascal  [Select][+][-]
  1. for Filler in Boolean do
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018