How to resolve the algorithm Count occurrences of a substring step by step in the Delphi programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Count occurrences of a substring step by step in the Delphi programming language
Table of Contents
Problem Statement
Create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string. The function should take two arguments:
It should return an integer count. 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).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Count occurrences of a substring step by step in the Delphi programming language
Source code in the delphi programming language
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.
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the jq programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Clojure programming language
You may also check:How to resolve the algorithm Pi step by step in the JavaScript programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the R programming language
You may also check:How to resolve the algorithm Nested function step by step in the Objective-C programming language