Jump to content

Count occurrences of a substring: Difference between revisions

Line 101:
<pre>3
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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.