- C
- C#
- D
- Elixir
- Erlang
- Fortran
- Go
- Haskell
- Haskell
- JS
- Java
- Java
- Java
- Java
- Lisp
- PHP
- Pascal
- Perl
- Python
- Ruby
- Rust
unsigned n;
for (n = 0; s = strstr(s, t); ++n, ++s)
;
Overlapping occurrences are counted.
This destroys the pointer s.
This destroys the pointer s.
countOccurence(List1, List2) ->
countOccurence(List1, List2, 0).
countOccurence(_, [], Count) ->
Count;
countOccurence(List1, [_ | Rest] = List2, Count) ->
case (lists:prefix(List1, List2)) of
true ->
countOccurence(List1, Rest, Count + 1);
false ->
countOccurence(List1, Rest, Count)
end.
countOccurence("ab", "abcddababa").
counts overlapping occurences
length . filter (isPrefixOf t) . tails $ s
Implemented using mostly point free style. Overlapping occurrences are counted.
import static java.util.regex.Pattern.compile;
import static java.util.regex.Pattern.quote;
import java.util.regex.Matcher;
int z = 0;
Matcher m = compile(quote(t)).matcher(s);
while (m.find()) ++z;
import static java.util.regex.Pattern.compile;
import static java.util.regex.Pattern.quote;
import java.util.regex.Matcher;
int z, i = z = 0;
Matcher m = compile(quote(t)).matcher(s);
while (m.find(i)) {
++z;
i = m.start() + 1;
}
This will count overlapped values.
function Count(t, s: String): Integer;
var
Offset, P: Integer;
begin
Result := 0;
Offset := 1;
P := PosEx(t, s, Offset);
while P > 0 do
begin
Inc(Result);
P := PosEx(t, s, P + 1);
end;
end;
Strings in Pascal start at index 1, therefore Offset is set to 1.
The function counts overlapping occurrences.
The function counts overlapping occurrences.
my $t="banana bo bana bandana";
my $c =()= $t=~ m/ana/g;
print "count without overlap: $c\n";
$c =()= $t =~ m/an(?=a)/g;
print "count with overlap: $c\n";
The =()= operator makes the match operate in list context (http://www.perlmonks.org/?node_id=527973), and the (?=) operator allows it to match overlapping text (surround the part that can overlap).