Count occurrences of a substring: Difference between revisions

Content added Content deleted
(added Fortran)
(Added PicoLisp)
Line 182: Line 182:
say count-substring("ababababab","abab");</lang>
say count-substring("ababababab","abab");</lang>
Note that in Perl 6 the <tt>/$little/</tt> matches the variable literally, so there's no need to quote regex metacharacters. Also, prefix <tt>+</tt> forces numeric context in Perl&nbsp;6 (it's a no-op in Perl&nbsp;5). One other style point: we now tend to prefer hyphenated names over camelCase.
Note that in Perl 6 the <tt>/$little/</tt> matches the variable literally, so there's no need to quote regex metacharacters. Also, prefix <tt>+</tt> forces numeric context in Perl&nbsp;6 (it's a no-op in Perl&nbsp;5). One other style point: we now tend to prefer hyphenated names over camelCase.

=={{header|PicoLisp}}==
<lang PicoLisp>(de countSubstring (Str Sub)
(let (Cnt 0 H (chop Sub))
(for (S (chop Str) S (cdr S))
(when (head H S)
(inc 'Cnt)
(setq S (map prog2 H S)) ) )
Cnt ) )</lang>
Test:
<pre>: (countSubstring "the three truths" "th")
-> 3

: (countSubstring "ababababab" "abab")
-> 2</pre>


=={{header|PureBasic}}==
=={{header|PureBasic}}==