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

Source code in the simula programming language

BEGIN
  
    INTEGER PROCEDURE COUNTSUBSTRING(T,TSUB); TEXT T,TSUB;
    BEGIN
        INTEGER N,I;
        I := 1;
        WHILE I+TSUB.LENGTH-1 <= T.LENGTH DO
          IF T.SUB(I,TSUB.LENGTH) = TSUB THEN
          BEGIN
              N := N+1;
              I := I+MAX(TSUB.LENGTH,1);
          END ELSE I := I+1;
        COUNTSUBSTRING:= N;
    END COUNTSUBSTRING;

    OUTINT(COUNTSUBSTRING("THE THREE TRUTHS", "TH"),0);
    OUTIMAGE;
    OUTINT(COUNTSUBSTRING("ABABABABAB", "ABAB"),0);
    OUTIMAGE;
END.

  

You may also check:How to resolve the algorithm Binary digits step by step in the S-BASIC programming language
You may also check:How to resolve the algorithm N'th step by step in the ERRE programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the Maxima programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the D programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the Pascal programming language