Count occurrences of a substring: Difference between revisions
m
no edit summary
(easylang) |
mNo edit summary |
||
(13 intermediate revisions by 7 users not shown) | |||
Line 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/>
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>
<syntaxhighlight lang="algol68">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 445 ⟶ 441:
);
print((
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
))
{{out}}
<pre>
3 2 2
Line 1,388 ⟶ 1,384:
print count "11111111" "12"
print count "12" "12"
</syntaxhighlight>
Line 1,555 ⟶ 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 2,133 ⟶ 2,151:
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln len indices
writeln len indices
{{out}}
Line 3,229 ⟶ 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,311 ⟶ 3,344:
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪
≪
'''
'''END'''
≫ ≫ '<span style="color:blue">CNTSUB</span>' 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,421 ⟶ 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,751 ⟶ 3,812:
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./fmt" for Fmt
Line 3,783 ⟶ 3,844:
Alternatively, using a library method (output same as before):
{{libheader|Wren-str}}
<syntaxhighlight lang="
import "./fmt" for Fmt
|