Count occurrences of a substring: Difference between revisions
Content added Content deleted
Line 101: | Line 101: | ||
<pre>3 |
<pre>3 |
||
2</pre> |
2</pre> |
||
=={{header|Icon}} and {{header|Unicon}}== |
|||
<lang Icon>procedure main() |
|||
every A := ![ ["the three truths","th"], ["ababababab","abab"] ] do |
|||
write("The string ",image(A[2])," occurs as a non-overlapping substring ", |
|||
countSubstring!A , " times in ",image(A[1])) |
|||
end |
|||
procedure countSubstring(s1,s2) #: return count of non-overlapping substrings |
|||
c := 0 |
|||
s1 ? while tab(find(s2)) do { |
|||
move(*s2) |
|||
c +:= 1 |
|||
} |
|||
return c |
|||
end</lang> |
|||
Output:<pre>The string "th" occurs as a non-overlapping substring 3 times in "the three truths" |
|||
The string "abab" occurs as a non-overlapping substring 2 times in "ababababab"</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |