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

Source code in the setl programming language

program count_overlapping_substrings;
    tests := [["the three truths", "th"], ["ababababab", "abab"]];
    loop for [s, subs] in tests do
        print("'" + subs + "' in '" + s + "': "
              + str countSubs(s, subs));
    end loop;

    proc countSubs(s, subs);
        count := 0;
        loop while s(subs) /= om do
            s(subs) := "";
            count +:= 1;
        end loop;
        return count;
    end proc;
end program;

  

You may also check:How to resolve the algorithm Fractal tree step by step in the TypeScript programming language
You may also check:How to resolve the algorithm Check Machin-like formulas step by step in the Tcl programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the J programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the FOCAL programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Lang programming language