Count occurrences of a substring: Difference between revisions

Add Macro-11
(Added Gambas)
(Add Macro-11)
Line 1,968:
print(countSubstring("the three truths", "th"))
print(countSubstring("ababababab", "abab"))</syntaxhighlight>
<pre>3
2</pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE CSUBS
.MCALL .TTYOUT,.EXIT
CSUBS:: JMP DEMO
; COUNT SUBSTRINGS R1 IN R0
COUNT: CLR R2
BR 4$
1$: MOV R0,R3
MOV R1,R4
2$: CMPB (R3)+,(R4)+
BEQ 2$
TSTB -(R4)
BNE 3$
INC R2
DEC R3
MOV R3,R0
BR 4$
3$: INC R0
4$: TSTB (R0)
BNE 1$
RTS PC
; TEST EXAMPLES
DEMO: MOV #ST1,R0
MOV #SU1,R1
JSR PC,1$
MOV #ST2,R0
MOV #SU2,R1
JSR PC,1$
.EXIT
1$: JSR PC,COUNT
ADD #60,R2
MOVB R2,3$
MOV #3$,R2
2$: MOVB (R2)+,R0
.TTYOUT
BNE 2$
RTS PC
3$: .BYTE 0,15,12,0
 
ST1: .ASCIZ /THE THREE TRUTHS/
SU1: .ASCIZ /TH/
ST2: .ASCIZ /ABABABABAB/
SU2: .ASCIZ /ABAB/
.END CSUBS</syntaxhighlight>
{{out}}
<pre>3
2</pre>
2,094

edits