Count occurrences of a substring: Difference between revisions

Content added Content deleted
(→‎{{header|RPL}}: simple, idiomatic version)
Line 3,299: Line 3,299:
=={{header|RPL}}==
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{{works with|Halcyon Calc|4.2.7}}
DUP2s t
DUP SIZE 1 -str substr subsize
SIZE SWAP SIZE
0
'''IF''' DUP2 * LAST AND '''THEN'''
1 str SIZE subsize + '''FOR''' j
SWAP - 1 + 0 R→C
str j DUP subsize + SUB
'''WHILE''' DUP RE 0 > '''REPEAT'''
'''IF''' substr == '''THEN'''
s OVER RE DUP t SIZE + 1 - SUB
1 +
'''IF''' t == '''THEN''' t SIZE -1 R→C - '''ELSE''' (1,0) - '''END'''
j subsize + 'j' STO
'''END'''
'''END'''
IM
'''NEXT'''
≫ ≫ '<span style="color:blue">CNTSUB</span>' STO
'''ELSE''' DROP2 -1 '''END'''

≫ ≫ ''''CNTSUB'''' STO
"the three truths" <span style="color:blue">CNTSUB</span>
"ababababab" <span style="color:blue">CNTSUB</span>
{{out}}
<pre>
2: 3
1: 2
</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==