#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# String Occurence Function

Posts: 6Member
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?
· ·

• Posts: 6,349Member
: 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:
[code]
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;
[/code]

· ·
• Posts: 6Member
: : 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:
: [code]
: 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;
: [/code]
:

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

· ·
• Posts: 6,349Member
: : : 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:
: : [code]
: : 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;
: : [/code]
: :
:
:
: 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.
· ·