## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

String Occurence Function Posted by martin5150 on 3 Jun 2005 at 1:17 PM
I'm having trouble thinking up a good algorithm for a function called "occurrence" that will return a byte of how many times a substring occurs in a string. I want the function as small as possible...

Can anyone help?
Re: String Occurence Function Posted by zibadian on 4 Jun 2005 at 12:46 AM
: I'm having trouble thinking up a good algorithm for a function called "occurrence" that will return a byte of how many times a substring occurs in a string. I want the function as small as possible...
:
: Can anyone help?
:
Here is a simple, untested function:
```function Occurance(SubStr, S: string): integer;
begin
Result := 0;
while Pos(SubStr, S) > 0 do { Find first occurance of SubStr in S }
begin
Result := Result + 1;
Delete(S, 1, Pos(SubStr, S)+Length(SubStr)-1);
{ Delete everyting upto and including previously found SubStr }
end;
end;
```

Re: String Occurence Function Posted by martin5150 on 4 Jun 2005 at 8:58 PM
: : I'm having trouble thinking up a good algorithm for a function called "occurrence" that will return a byte of how many times a substring occurs in a string. I want the function as small as possible...
: :
: : Can anyone help?
: :
: Here is a simple, untested function:
:
```: function Occurance(SubStr, S: string): integer;
: begin
:   Result := 0;
:   while Pos(SubStr, S) > 0 do { Find first occurance of SubStr in S }
:   begin
:     Result := Result + 1;
:     Delete(S, 1, Pos(SubStr, S)+Length(SubStr)-1);
:      { Delete everyting upto and including previously found SubStr }
:   end;
: end;
: ```

:

Thanks! That's exactly what I was looking for. I thought it would be more difficult than that...

Re: String Occurence Function Posted by zibadian on 4 Jun 2005 at 9:03 PM
: : : I'm having trouble thinking up a good algorithm for a function called "occurrence" that will return a byte of how many times a substring occurs in a string. I want the function as small as possible...
: : :
: : : Can anyone help?
: : :
: : Here is a simple, untested function:
: :
```: : function Occurance(SubStr, S: string): integer;
: : begin
: :   Result := 0;
: :   while Pos(SubStr, S) > 0 do { Find first occurance of SubStr in S }
: :   begin
: :     Result := Result + 1;
: :     Delete(S, 1, Pos(SubStr, S)+Length(SubStr)-1);
: :      { Delete everyting upto and including previously found SubStr }
: :   end;
: : end;
: : ```

: :
:
:
: Thanks! That's exactly what I was looking for. I thought it would be more difficult than that...
:
:
There are other ways. You could also loop through the string and compare the substr to a part of the s using Copy(). Or you could typecast both as PChars and use a memory compare to compare the parts.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic