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

Source code in the draco programming language

proc countSubstring(*char string, substring) word:
    word count;
    *char pos, loc;
    
    count := 0;
    while string* /= '\e' do
        pos := substring;
        loc := string;
        while loc* = pos* do
            loc := loc + 1;
            pos := pos + 1
        od;
        if pos* = '\e' then
            string := loc;
            count := count + 1
        else
            string := string + 1
        fi
    od;
    count
corp

proc main() void:
    writeln(countSubstring("the three truths", "th"));
    writeln(countSubstring("ababababab", "abab"))
corp

  

You may also check:How to resolve the algorithm Price fraction step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Empty string step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the PHP programming language
You may also check:How to resolve the algorithm Mertens function step by step in the Modula-2 programming language