How to resolve the algorithm Count occurrences of a substring step by step in the BCPL 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 BCPL 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 BCPL programming language

Source code in the bcpl programming language

get "libhdr"

let countsubstr(str, match) = valof
$(  let i, count = 1, 0
    while i <= str%0 do
        test valof
        $(  for j = 1 to match%0
                unless match%j = str%(i+j-1)
                    resultis false
            resultis true
        $)
        then
        $(  count := count + 1
            i := i + match%0
        $)
        else
            i := i + 1
    resultis count
$)

let show(str, match) be
    writef("*"%S*" in *"%S*": %N*N", 
        match, str, countsubstr(str, match))

let start() be
$(  show("the three truths", "th")
    show("ababababab", "abab")
    show("cat", "dog")
$)

  

You may also check:How to resolve the algorithm Euler's identity step by step in the Maxima programming language
You may also check:How to resolve the algorithm Bitmap/PPM conversion through a pipe step by step in the Raku programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the hexiscript programming language
You may also check:How to resolve the algorithm Generic swap step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm URL decoding step by step in the Icon and Unicon programming language