Jump to content

Count occurrences of a substring: Difference between revisions

Added Delphi example
(Added Delphi example)
Line 101:
<pre>3
2</pre>
 
=={{header|Delphi}}==
<lang Delphi>program OccurrencesOfASubstring;
 
{$APPTYPE CONSOLE}
 
uses StrUtils;
 
function CountSubstring(const aString, aSubstring: string): Integer;
var
lPosition: Integer;
begin
Result := 0;
lPosition := PosEx(aSubstring, aString);
while lPosition <> 0 do
begin
Inc(Result);
lPosition := PosEx(aSubstring, aString, lPosition + Length(aSubstring));
end;
end;
 
begin
Writeln(CountSubstring('the three truths', 'th'));
Writeln(CountSubstring('ababababab', 'abab'));
end.</lang>
 
=={{header|Go}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.