Count occurrences of a substring: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|ALGOL 68}}: Add note about string in string, remove use of formatted transput to make runable with more compilers/interpreters)
Line 430: Line 430:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Algol68 has no build in function to do this task, hence the next 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;
INT start:=LWB haystack, next, out:=0;
FOR count WHILE string in string(needle, next, haystack[start:]) DO
FOR count WHILE string in string(needle, next, haystack[start:]) DO
Line 445: Line 441:
);
);


print((
printf(($d" "$,
count string in string("th", "the three truths"), # expect 3 #
whole( count string in string("th", "the three truths"), 0 ) # expect 3 #, " ",
count string in string("abab", "ababababab"), # expect 2 #
whole( count string in string("abab", "ababababab"), 0 ) # expect 2 #, " ",
count string in string("a*b", "abaabba*bbaba*bbab"), # expect 2 #
whole( count string in string("a*b", "abaabba*bbaba*bbab"), 0 ) # expect 2 #, newline
))
$l$
))</syntaxhighlight>
</syntaxhighlight>
{{out}}

<pre>
<pre>
3 2 2
3 2 2