Count occurrences of a substring: Difference between revisions
Add BCPL
Not a robot (talk | contribs) (Add PL/M) |
Not a robot (talk | contribs) (Add BCPL) |
||
Line 576:
<pre>3 "th" in "the three truths"
2 "abab" in "ababababab"</pre>
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
let countsubstr(str, match) = valof
$( let i, count = 1, 0
while i <= str%0 do
test valof
$( for j = 1 to match%0
unless match%j = str%(i+j-1)
resultis false
resultis true
$)
then
$( count := count + 1
i := i + match%0
$)
else
i := i + 1
resultis count
$)
let show(str, match) be
writef("*"%S*" in *"%S*": %N*N",
match, str, countsubstr(str, match))
let start() be
$( show("the three truths", "th")
show("ababababab", "abab")
show("cat", "dog")
$)</lang>
{{out}}
<pre>"th" in "the three truths": 3
"abab" in "ababababab": 2
"dog" in "cat": 0</pre>
=={{header|Bracmat}}==
|