Count occurrences of a substring: Difference between revisions
Content deleted Content added
m →{{header|Ruby}}: Stop using Array#count, because it needs Ruby 1.8.7. |
added ocaml |
||
Line 369:
<pre>3
2</pre>
=={{header|OCaml}}==
<lang ocaml>let count_substring str sub =
let sub_len = String.length sub in
let len_diff = (String.length str) - sub_len
and reg = Str.regexp_string sub in
let rec aux i n =
if i > len_diff then n else
try
let pos = Str.search_forward reg str i in
aux (pos + sub_len) (succ n)
with Not_found -> n
in
aux 0 0
let () =
Printf.printf "count 1: %d\n" (count_substring "the three truth" "th");
Printf.printf "count 2: %d\n" (count_substring "ababababab" "abab");
;;</lang>
=={{header|Perl}}==
Line 381 ⟶ 401:
print countSubstring("the three truths","th"), "\n"; # prints "3"
print countSubstring("ababababab","abab"), "\n"; # prints "2"</lang>
=={{header|Perl 6}}==
<lang perl6>sub count-substring($big,$little) { +$big.comb: /$little/ }
|