Difference between revisions of "Count occurrences of a substring"
(Creation of page)
Revision as of 13:41, 3 October 2019
Description (from Rosetta Code)
Create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string.
The function should take two arguments:
It should return an integer count.
print countSubstring("the three truths","th") 3 // do not count substrings that overlap with previously-counted substrings: print countSubstring("ababababab","abab") 2
The matching should yield the highest number of non-overlapping matches.
In general, this essentially means matching from left-to-right or right-to-left.