Recent

Author Topic: match all occurences of 'cat' in a string  (Read 1014 times)

engkin

  • Hero Member
  • *****
  • Posts: 2431
Re: match all occurences of 'cat' in a string
« Reply #15 on: June 13, 2019, 04:18:57 am »
@engkin

The call was made:
Var
NilCount : Integer;

Begin
NilCount := Count('Nil',[KPHX][123548]Niles][Nil][United States][62.458][-123.458]);

Thus I think you would expect a return value of 1;
Why 1, there are two, see:
[KPHX][123548]Niles][Nil][United States][62.458][-123.458]

Bart

  • Hero Member
  • *****
  • Posts: 3439
    • Bart en Mariska's Webstek
Re: match all occurences of 'cat' in a string
« Reply #16 on: June 13, 2019, 10:38:18 pm »
Begin
NilCount := Count('Nil',[KPHX][123548]Niles][Nil][United States][62.458][-123.458]);

Thus I think you would expect a return value of 1;

Why would you doubt my code?
I'm always 100% error free, as everybody around here knows  O:-)

Bart

BobS

  • Full Member
  • ***
  • Posts: 153
Re: match all occurences of 'cat' in a string
« Reply #17 on: June 14, 2019, 05:14:03 am »
You can always make use of the many string functions included with Free Pascal:

Code: Pascal  [Select]
  1. program TestApp;
  2.  
  3. uses strutils;
  4.  
  5. var
  6.   x, y: integer;
  7.   SearchFor, SearchIn: string;
  8.  
  9. begin
  10.    y:=1;
  11.    SearchFor:='cat';
  12.    searchIn:= 'thecatibthezcatzbwcatwcatacatycatoidxcatzokay';
  13.    repeat
  14.       x:=nPos(SearchFor, SearchIn, y);
  15.       if x>0
  16.          then
  17.             writeln (SearchFor,' found at: ',x);
  18.       inc(y);
  19.    until x=0;
  20. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 8082
Re: match all occurences of 'cat' in a string
« Reply #18 on: June 14, 2019, 08:34:39 am »
I miss the helper solution:
Code: Pascal  [Select]
  1.  
  2. {$mode objfpc}{$H+}
  3. uses sysutils;
  4. var
  5.   i:SizeInt = 0;
  6.   c:integer = 0;
  7.   s1:string = 'catthecatibthezcatzbwcatwcatacatycatoidxcatzokay';
  8.   s2:string = 'cat';
  9. begin  
  10.   repeat
  11.     i:=s1.IndexOf(s2,i);
  12.     if i > -1 then
  13.     begin
  14.       inc(c);
  15.       writeln(i:4,c:4);
  16.       inc(i);
  17.     end;
  18.   until i < 0;
  19. end.

« Last Edit: June 14, 2019, 08:41:36 am by Thaddy »
Hamlet 1.4 (nothing wrong with the Danish, btw)