Recent

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

frederic

  • Full Member
  • ***
  • Posts: 226
find word in string
« on: July 20, 2017, 02:19:28 pm »
dear specialists,

like in all search procedures there is a standard way of separating a group of connected characters from a larger string.

before i start my own design ,my question

where can i find something like this?

frederic

ahiggins

  • Jr. Member
  • **
  • Posts: 92
Re: find word in string
« Reply #1 on: July 20, 2017, 02:26:50 pm »
Could you use POS and COPY or am I miss understanding the question?

frederic

  • Full Member
  • ***
  • Posts: 226
Re: find word in string
« Reply #2 on: July 20, 2017, 03:00:34 pm »
Quote
Could you use POS and COPY or am I miss understanding the question?

an example:
totalstring:= 'ddsgdtervcterterve';
astringtofind:='terv';

function should return :yes :'terv' is part of totalstring
...................more advanced:
Yes:'terv' is 2 times part of total string

but this is so basic is must be available somewhere

frederic

ahiggins

  • Jr. Member
  • **
  • Posts: 92
Re: find word in string
« Reply #3 on: July 20, 2017, 03:12:06 pm »
how about AnsiMatchText  in strutils

Tony

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: find word in string
« Reply #4 on: July 20, 2017, 03:12:23 pm »
It is basic. It is also a basic question, but the count part is something you have to write yourself..., using PosEx...
Specialize a type, not a var.

frederic

  • Full Member
  • ***
  • Posts: 226
Re: find word in string
« Reply #5 on: July 20, 2017, 03:24:13 pm »
Quote
how about AnsiMatchText  in strutils

indeed,that is where i was looking for
thanks ,ahiggins

Quote
but the count part is something you have to write yourself..., using PosEx...

oke,thanks thaddy

ahiggins

  • Jr. Member
  • **
  • Posts: 92
Re: find word in string
« Reply #6 on: July 20, 2017, 03:30:24 pm »
found this online may help

Code: Pascal  [Select][+][-]
  1. { Returns a count of the number of occurences of SubText in Text }
  2. function CountOccurences( const SubStr: string; const Str: string): Integer;
  3. begin
  4.   Result := Pos(SubStr, Str);
  5.   if Result > 0 then
  6.     Result := (Length(Str) - Length(StringReplace(Str, SubStr, '', [rfReplaceAll]))) div Length(SubStr);
  7. end;  { CountOccurences }
  8.  

fred

  • Full Member
  • ***
  • Posts: 201
Re: find word in string
« Reply #7 on: July 20, 2017, 03:40:51 pm »
This might work also:

Code: Pascal  [Select][+][-]
  1. function CountString(const astringtofind, totalstring: string): integer;
  2. var
  3.    p: integer;
  4. begin
  5.    Result := 0;
  6.    p := 1;
  7.    repeat
  8.      p := PosEx(astringtofind, totalstring, p);
  9.      if (p > 0)
  10.      then begin Inc(Result); p := p+length(astringtofind); end;
  11.    until p = 0;
  12. end;
« Last Edit: July 20, 2017, 03:42:34 pm by fred »

ahiggins

  • Jr. Member
  • **
  • Posts: 92
Re: find word in string
« Reply #8 on: July 20, 2017, 03:56:58 pm »
my clumsy attempt, i'm sure there must be a more elegant way

Code: Pascal  [Select][+][-]
  1.  
  2. function CountOccurences( const SubStr: string; const Str: string): Integer;
  3. var
  4.   Count : Word = 0;  // word count
  5.   I     : Word = 1;  // string index
  6.   L     : Word;     //length of sub str
  7. begin
  8.      L:=Length(SubStr);
  9.      While I > 0 do
  10.      begin
  11.           I:=PosSetEx(SubStr,Str,I);
  12.           If  I > 0 Then
  13.            begin
  14.                 Inc(I,L);
  15.                 Inc(Count);
  16.            end;
  17.      end;
  18.  
  19.      Result:=Count;
  20. end;
  21.  
  22.  


engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: find word in string
« Reply #9 on: July 20, 2017, 06:11:09 pm »
Code: Pascal  [Select][+][-]
  1.    Str := 'abababa';
  2. SubStr := 'aba';

Count = 2, or Count = 3?

Code: Pascal  [Select][+][-]
  1. abababa
  2. aba
  3.     aba

Code: Pascal  [Select][+][-]
  1. abababa
  2. aba
  3.   aba
  4.     aba

ahiggins

  • Jr. Member
  • **
  • Posts: 92
Re: find word in string
« Reply #10 on: July 20, 2017, 06:30:05 pm »
@engkin I guess my attempt was more than just clumsy.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: find word in string
« Reply #11 on: January 16, 2019, 04:04:52 am »
My first code block.

Code: Pascal  [Select][+][-]
  1. program tally;
  2.  
  3. // =========  number of partstring in somestring =============//
  4.  function tally(somestring:pchar;partstring:pchar ):integer;
  5. var
  6. i,j,ln,lnp,count:integer;
  7. label
  8. skip;
  9. begin
  10. ln:=length(somestring);
  11. lnp:=length(partstring);
  12. count:=0;
  13. for i:=0 to ln-1 do
  14. begin
  15.    if somestring[i] <> partstring[0] then goto skip ;
  16.      if somestring[i] = partstring[0] then
  17.      begin
  18.      for j:=0 to lnp-1 do
  19.      begin
  20.      if somestring[j+i]<>partstring[j] then goto skip;
  21.      end;
  22.       count+=1;
  23.      end ;
  24.    skip:
  25. end;
  26.   tally:=count;
  27. end; {tally}
  28.  
  29.     //=========== Trial =========== //
  30.  
  31.  
  32.  var
  33.  p:pchar;
  34.  s:ansistring;
  35.  i,num:integer;
  36.  begin
  37.  s:='abababa' ;
  38.  for i:=1 to 18 do
  39.  begin
  40.  s+=s;
  41.  end;
  42.  
  43.  p:=pchar(s);        // cast
  44.  
  45.  num:=tally(p,'aba');
  46.  writeln('Tally');
  47.  writeln(num);
  48.  
  49.  write('string length  =  ');
  50.  writeln(length(p));
  51.  writeln('Press enter to end');
  52.  readln;
  53.  
  54.  end.
  55.  
  56.    

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: find word in string
« Reply #12 on: January 16, 2019, 04:56:33 am »
My first code block.

[... some code ... ]


One question (pure curiosity): Why do you use "goto skip" instead of the more formal "continue" (or "break" in the second case)?
« Last Edit: January 16, 2019, 04:58:07 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

guest63552

  • Guest
Re: find word in string
« Reply #13 on: January 16, 2019, 07:36:44 am »
There is many problems with the code, not just using "goto" and "lables".

- In structured languages, avoid using goto" and "lables" as that makes code really messy. It fits with assembler, not with structured higher level languages
- Your arguments are pointers, however you use indexes to access value. Use pointer arithmetic, which is much faster.
- Inner loop do not handle situation when there less characters are left in searching string than actually have string which is searched.
- Avoid usage of C like operators (+= and similar) in Pascal program, they are specific for FPC only.
- Avoid using "s:=s+s1" constructions on large strings, as they will moderately slow execution during constant memory reallocation. In your case that can be consider irrelevant (7*18 chars) made for test only, however in real world applications with large amount to concat, that can be extremely slow.
- And return value of function using Result. Returning it through function name is old and long time abandoned Turbo Pascal style.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: find word in string
« Reply #14 on: January 16, 2019, 12:25:54 pm »
Thank you for your comments.
I use 64 bit FreePascal  3.0.4 with an old dev-pas ide adapted.
I do not have the Lazarus ide.
I note that from the home page (www.freepascal.org) to the forum link, the page becomes Lazarus, am I perhaps in the wrong section/forum?

 

 

TinyPortal © 2005-2018