Jump to content

Count occurrences of a substring: Difference between revisions

Added solution for Action!
(Added solution for Action!)
Line 247:
 
<pre>3 2 0</pre>
 
=={{header|Action!}}==
<lang Action!>BYTE FUNC CountSubstring(CHAR ARRAY s,sub)
BYTE i,j,res,found
 
i=1 res=0
WHILE i-1+sub(0)<=s(0)
DO
found=1
FOR j=1 TO sub(0)
DO
IF s(j+i-1)#sub(j) THEN
found=0
EXIT
FI
OD
 
IF found=1 THEN
i==+sub(0)
res==+1
ELSE
i==+1
FI
OD
RETURN (res)
 
PROC Test(CHAR ARRAY s,sub)
BYTE c
 
c=CountSubstring(s,sub)
PrintF("%B ""%S"" in ""%S""%E",c,sub,s)
RETURN
 
PROC Main()
Test("the three truths","th")
Test("ababababab","abab")
Test("11111111","11")
Test("abcdefg","123")
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Count_occurrences_of_a_substring.png Screenshot from Atari 8-bit computer]
<pre>
3 "th" in "the three truths"
2 "abab" in "ababababab"
4 "11" in "11111111"
0 "123" in "abcdefg"
</pre>
 
=={{header|Ada}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.