How to resolve the algorithm Count occurrences of a substring step by step in the PL/I 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 PL/I 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 PL/I programming language
Source code in the pl/i programming language
cnt: procedure options (main);
declare (i, tally) fixed binary;
declare (text, key) character (100) varying;
get edit (text) (L); put skip data (text);
get edit (key) (L); put skip data (key);
tally = 0; i = 1;
do until (i = 0);
i = index(text, key, i);
if i > 0 then do; tally = tally + 1; i = i + length(key); end;
end;
put skip list (tally);
end cnt;
You may also check:How to resolve the algorithm Vector products step by step in the Ring programming language
You may also check:How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Java programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Forth programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Clojure programming language
You may also check:How to resolve the algorithm Permutations by swapping step by step in the Wren programming language