Pascal

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

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
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?
Report
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;


Report
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...

Report
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
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.