Count occurrences of a substring: Difference between revisions

m
no edit summary
mNo edit summary
 
(17 intermediate revisions by 8 users not shown)
Line 247:
 
<pre>3 2 0</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program strcptsub64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessResult: .asciz "Result: "
szString: .asciz "the three truths"
szSubString: .asciz "th"
szString1: .asciz "ababababab"
szSubString1: .asciz "abab"
szCarriageReturn: .asciz "\n"
szMessStart: .asciz "Program 64 bits start.\n"
/************************************/
/* UnInitialized data */
/************************************/
.bss
sZoneConv: .skip 24
/************************************/
/* code section */
/************************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess
ldr x0,qAdrszString
ldr x1,qAdrszSubString
bl countSubString
ldr x0,qAdrszString1
ldr x1,qAdrszSubString1
bl countSubString
 
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc 0 // perform the system call
qAdrszString: .quad szString
qAdrszSubString: .quad szSubString
qAdrszString1: .quad szString1
qAdrszSubString1: .quad szSubString1
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessStart: .quad szMessStart
/***************************************************/
/* count sub string of string */
/***************************************************/
/* r0 contains a string */
/* r1 contains a substring */
/* r0 return substring count */
countSubString:
stp x1,lr,[sp,-16]!
stp x2,x3,[sp,-16]!
stp x4,x5,[sp,-16]!
stp x6,x7,[sp,-16]!
mov x4,#0 // counter
mov x3,#0 // index string
mov x5,#0 // index substring
1:
ldrb w6,[x0,x5] // load byte of string
ldrb w7,[x1,x3] // load byte of substring
cmp x6,x7 // compare byte
bne 2f // not equal
cmp x6,#0 // string end ?
beq 3f // yes
add x5,x5,#1 // else increment index
add x3,x3,#1
b 1b // and loop
2: // characters not equal
cmp x6,#0 // end string ?
beq 4f
cmp x7,#0 // end substring ?
add x6,x4,1
csel x4,x6,x4,eq // yes -> increment counter
mov x3,#0 // raz index substring
add x5,x5,#1 // increment string index
b 1b // and loop
3: // end string and end substring
add x4,x4,#1 // increment counter
4: // result display
mov x0,x4
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrszMessResult
bl affichageMess
ldr x0,qAdrsZoneConv
bl affichageMess
ldr x0,qAdrszCarriageReturn
bl affichageMess
mov x0,x4
100:
ldp x6,x7,[sp],16
ldp x4,x5,[sp],16
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16
ret
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
Result: 3
Result: 2
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE FUNC CountSubstring(CHAR ARRAY s,sub)
Line 310 ⟶ 430:
 
=={{header|ALGOL 68}}==
Algol68 has no build in function to do this task, hence the need to create a ''count string in string'' routine.<br/>
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}}
If your Algol 68 compiler/interpreter does not have ''string in string'', there is an implementation on Rosetta Code [[ALGOL_68/prelude#string_in_string|here]].<br>
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
<syntaxhighlight lang="algol68">PROC count string in string = (STRING needle, haystack)INT: (
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
Algol68 has no build in function to do this task, hence the next to create a ''count string in string'' routine.
<syntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
 
PROC count string in string = (STRING needle, haystack)INT: (
INT start:=LWB haystack, next, out:=0;
FOR count WHILE string in string(needle, next, haystack[start:]) DO
Line 325 ⟶ 441:
);
 
print((
printf(($d" "$,
whole( count string in string("th", "the three truths"), 0 ) # expect 3 #, " ",
whole( count string in string("abab", "ababababab"), 0 ) # expect 2 #, " ",
whole( count string in string("a*b", "abaabba*bbaba*bbab"), 0 ) # expect 2 #, newline
))
$l$
))</syntaxhighlight>
{{out}}
 
<pre>
3 2 2
Line 424 ⟶ 540:
{{Out}}
<syntaxhighlight lang="applescript">{3, 2}</syntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program strcptsub.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../constantes.inc"
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessResult: .asciz "Result: "
szString: .asciz "the three truths"
szSubString: .asciz "th"
szString1: .asciz "ababababab"
szSubString1: .asciz "abab"
szCarriageReturn: .asciz "\n"
szMessStart: .asciz "Program 32 bits start.\n"
/************************************/
/* UnInitialized data */
/************************************/
.bss
sZoneConv: .skip 24
/************************************/
/* code section */
/************************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessStart
bl affichageMess
ldr r0,iAdrszString
ldr r1,iAdrszSubString
bl countSubString
ldr r0,iAdrszString1
ldr r1,iAdrszSubString1
bl countSubString
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform the system call
iAdrszString: .int szString
iAdrszSubString: .int szSubString
iAdrszString1: .int szString1
iAdrszSubString1: .int szSubString1
iAdrsZoneConv: .int sZoneConv
iAdrszMessResult: .int szMessResult
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessStart: .int szMessStart
/***************************************************/
/* count sub string of string */
/***************************************************/
/* r0 contains a string */
/* r1 contains a substring */
/* r0 return substring count */
countSubString:
push {r1-r7,lr} @ save registers
mov r4,#0 @ counter
mov r3,#0 @ index string
Mov r5,#0 @ index substring
1:
ldrb r6,[r0,r5] @ load byte of string
ldrb r7,[r1,r3] @ load byte of substring
cmp r6,r7 @ compare byte
bne 2f @ not equal
cmp r6,#0 @ string end ?
beq 3f @ yes
add r5,r5,#1 @ else increment index
add r3,r3,#1
b 1b @ and loop
2: @ characters not equal
cmp r6,#0 @ end string ?
beq 4f
cmp r7,#0 @ end substring ?
addeq r4,r4,#1 @ yes -> increment counter
mov r3,#0 @ raz index substring
add r5,r5,#1 @ increment string index
b 1b @ and loop
3: @ end string and end substring
add r4,r4,#1 @ increment counter
4: @ result display
mov r0,r4
ldr r1,iAdrsZoneConv
bl conversion10
ldr r0,iAdrszMessResult
bl affichageMess
ldr r0,iAdrsZoneConv
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
mov r0,r4
100:
pop {r1-r7,pc}
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../affichage.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
Program 32 bits start.
Result: 3
Result: 2
</pre>
=={{header|Arturo}}==
 
Line 1,136 ⟶ 1,364:
<pre>3
2</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func count str$ pat$ .
ind = 1
while ind + len pat$ - 1 <= len str$
if substr str$ ind len pat$ = pat$
cnt += 1
ind += len pat$
else
ind += 1
.
.
return cnt
.
print count "the three truths" "th"
print count "ababababab" "abab"
print count "11111111" "11"
print count "11111111" "12"
print count "12" "12"
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,290 ⟶ 1,539:
0
0
</pre>
 
=={{header|Emacs Lisp}}==
Two Emacs Lisp solutions are shown below
<syntaxhighlight lang="lisp">
;; version 1, which takes advantage of the how-many function,
;; which runs only in a buffer
 
(defun count-substrings (text substring)
"Count non-overlapping occurrences of SUBSTRING in TEXT."
(with-temp-buffer ; create a temporary buffer, which will be deleted when function finishes
(insert text) ; insert TEXT into the empty buffer
(goto-char (point-min)) ; go to the beginning of the buffer
(how-many substring))) ; count how many occurrences of SUBSTRING
 
 
;; version 2, which operates on a string
 
(defun count-substrings (text substring)
"Count non-overlapping occurences of SUBSTRING in TEXT."
(let ((substrings)) ; empty list to add occurrences of SUBSTRING as we find them
(while (string-match substring text) ; as long as we can find SUBSTRING in TEXT
(push (match-string 0 text) substrings) ; add the SUBSTRING we found to the list of substrings
(setq text (replace-match "" nil nil text))) ; remove SUBSTRING from text, and repeat while loop
(length substrings))) ; count number of items in substrings list
</syntaxhighlight>
{{out}}
<pre>
(count-substrings "the three truths" "th")
3
 
(count-substrings "ababababab" "abab")
2
 
</pre>
 
Line 1,868 ⟶ 2,151:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln len indices q("th)", q("the three truths)"
writeln len indices q("abab)", q("ababababab)"</syntaxhighlight>
 
{{out}}
Line 2,964 ⟶ 3,247:
ababababab - 2
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Count ('th') 'the three truths'>>
<Prout <Count ('abab') 'abababab'>>;
};
 
Count {
(e.item) e.item e.rest = <+ 1 <Count (e.item) e.rest>>;
(e.item) s.x e.rest = <Count (e.item) e.rest>;
(e.item) = 0;
};</syntaxhighlight>
{{out}}
<pre>3
2</pre>
 
=={{header|REXX}}==
Line 3,046 ⟶ 3,344:
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
DUP2DUP SIZE 1 -sstr substr tsubsize
SIZE SWAP SIZE 0
'''IF''' DUP21 *str LASTSIZE subsize AND+ '''THENFOR''' j
SWAP - 1str +j 0DUP subsize + R→CSUB
'''WHILEIF''' DUP RE 0substr >== '''REPEATTHEN'''
s OVER RE DUP t SIZE1 + 1 - SUB
'''IF''' t ==j '''THEN'''subsize t SIZE -1 R→C -+ '''ELSE''j' (1,0) - '''END'''STO
'''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}}==
Line 3,156 ⟶ 3,461:
2
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program count_overlapping_substrings;
tests := [["the three truths", "th"], ["ababababab", "abab"]];
loop for [s, subs] in tests do
print("'" + subs + "' in '" + s + "': "
+ str countSubs(s, subs));
end loop;
 
proc countSubs(s, subs);
count := 0;
loop while s(subs) /= om do
s(subs) := "";
count +:= 1;
end loop;
return count;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>'th' in 'the three truths': 3
'abab' in 'ababababab': 2</pre>
 
=={{header|SenseTalk}}==
Line 3,486 ⟶ 3,812:
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./pattern" for Pattern
import "./fmt" for Fmt
 
Line 3,518 ⟶ 3,844:
Alternatively, using a library method (output same as before):
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "./str" for Str
import "./fmt" for Fmt
 
27

edits