Count occurrences of a substring: Difference between revisions

Add Seed7 example
(Add Seed7 example)
Line 553:
 
String#scan returns an array of substrings, and Array#length (or Array#size) counts them.
 
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
 
const func integer: countSubstring (in string: stri, in string: searched) is func
result
var integer: count is 0;
local
var integer: offset is 0;
begin
offset := pos(stri, searched);
while offset <> 0 do
incr(count);
offset := pos(stri, searched, offset + length(searched));
end while;
end func;
const proc: main is func
begin
writeln(countSubstring("the three truths", "th"));
writeln(countSubstring("ababababab", "abab"));
end func;</lang>
 
Output:
<pre>
3
2
</pre>
 
=={{header|SNOBOL4}}==