n ocurrance in a string

David Medlock noone at nowhere.com
Thu Jan 4 06:24:06 PST 2007


Heinz wrote:
> Hi, thanks for your reply. But with this method i'd have to repeat it n times, if
> i want to find the 124th ocurrance, that would take a lot of time right?
> 
> Anyway i find it a straight foward method, i think it's the best way to go. This
> is a very needed function, i'll add a counter to check the n times it has to loop.
> I'll post it then (someone may need it).
> 
> Thanks in advance.

A few ideas that popped into my head.

Search for the last character in your substring, say you find it at N
when you find it check for your substring at N - substring.length.

Perhaps as you cycle through the string, keep a sum of the last X 
character values, where X is the length of your substring.  If your 
current SUM is not equal to the sum of the characters in your substring 
you don't have to perform the check above.

Perhaps check citeseer for string searching algorithms.

Good luck.
-DavidM



More information about the Digitalmars-d-learn mailing list