Count occurrences of a substring: Difference between revisions

Added Chipmunk Basic, GW-BASIC and MSX Basic
(RPL: add section)
(Added Chipmunk Basic, GW-BASIC and MSX Basic)
Line 367:
 
=={{header|AppleScript}}==
 
This is a good example of the kind of problem to which standard libraries (a regex library in this case) would offer most languages a simple and immediate solution.
AppleScript, however, for want of various basics like regex and math library functions, can require scripters to draw on the supplementary resources of Bash, using the built-in ''do shell script'' function.
Line 558 ⟶ 557:
180 NEXT I
190 RETURN</syntaxhighlight>
 
==={{header|BASIC256}}===
{{trans|Run BASIC}}
<syntaxhighlight lang="freebasic">print countSubstring("the three truths","th")
print countSubstring("ababababab","abab")
end
 
function countSubstring(s$,find$)
i = 1
while instr(s$,find$,i) <> 0
countSubstring += 1
i = instr(s$,find$,i) + length(find$)
end while
end function</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de Run BASIC.</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|Applesoft BASIC}}
{{works with|MSX_BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{trans|Applesoft BASIC}}
<syntaxhighlight lang="qbasic">10 CLS : REM 10 HOME for Applesoft BASIC
20 F$ = "TH"
30 S$ = "THE THREE TRUTHS"
40 GOSUB 110: REM COUNT SUBSTRING
50 PRINT R
60 F$ = "ABAB"
70 S$ = "ABABABABAB"
80 GOSUB 110: REM COUNT SUBSTRING
90 PRINT R
100 END
110 R = 0
120 F = LEN(F$)
130 S = LEN(S$)
140 IF F > S THEN RETURN
150 IF F = 0 THEN RETURN
160 IF F = S AND F$ = S$ THEN R = 1: RETURN
170 FOR I = 1 TO S - F
180 IF F$ = MID$(S$, I, F) THEN R = R + 1: I = I + F - 1
190 NEXT I
200 RETURN</syntaxhighlight>
 
==={{header|GW-BASIC}}===
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|IS-BASIC}}===
Line 571 ⟶ 617:
190 LET COUNT=N
200 END DEF</syntaxhighlight>
 
==={{header|MSX Basic}}===
The [[#Chipmunk_Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|Sinclair ZX81 BASIC}}===
Line 608 ⟶ 657:
PRINT "ababababab, abab:", countSubstring("ababababab", "abab")
END</syntaxhighlight>
 
==={{header|BASIC256}}===
{{trans|Run BASIC}}
<syntaxhighlight lang="freebasic">print countSubstring("the three truths","th")
print countSubstring("ababababab","abab")
end
 
function countSubstring(s$,find$)
i = 1
while instr(s$,find$,i) <> 0
countSubstring += 1
i = instr(s$,find$,i) + length(find$)
end while
end function</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de Run BASIC.</pre>
 
==={{header|Yabasic}}===
Line 642 ⟶ 675:
{{out}}
<pre>Igual que la entrada de Run BASIC.</pre>
 
 
=={{header|Batch File}}==
2,161

edits