Count occurrences of a substring: Difference between revisions

m
Alphabetized
(→‎{{header|C++}}: Added C++ implementation)
m (Alphabetized)
Line 11:
The matching should yield the highest number of non-overlapping matches. In general, this essentially means matching from left-to-right or right-to-left (see proof on talk page).
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}}
{{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].}}
{{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.
<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
start+:=next+UPB needle-LWB needle;
out:=count
OD;
out
);
 
printf(($d" "$,
count string in string("th", "the three truths"), # expect 3 #
count string in string("abab", "ababababab"), # expect 2 #
count string in string("a*b", "abaabba*bbaba*bbab"), # expect 2 #
$l$
))</lang>
Output:
<pre>
3 2 2
</pre>
=={{header|C++}}==
<lang cpp>#include <iostream>
Line 42 ⟶ 68:
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}}
{{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].}}
{{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.
<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
start+:=next+UPB needle-LWB needle;
out:=count
OD;
out
);
 
printf(($d" "$,
count string in string("th", "the three truths"), # expect 3 #
count string in string("abab", "ababababab"), # expect 2 #
count string in string("a*b", "abaabba*bbaba*bbab"), # expect 2 #
$l$
))</lang>
Output:
<pre>
3 2 2
</pre>
=={{header|Java}}==
This method removes the first occurrence of the substring, checks for changes, and continues if something changed. It counts each removal attempt (even if nothing is replaced), so the count starts at -1 to offset the last removal attempt where nothing changed.
Anonymous user