Count occurrences of a substring: Difference between revisions

 
(7 intermediate revisions by 2 users not shown)
Line 430:
 
=={{header|ALGOL 68}}==
Algol68 has no build in function to do this task, hence the nextneed 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 445 ⟶ 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 1,543 ⟶ 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,121 ⟶ 2,151:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln len (indices ("th", "the three truths"))
writeln len (indices ("abab", "ababababab"</syntaxhighlight>))
</syntaxhighlight>
 
{{out}}
1,007

edits