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

Source code in the rexx programming language

/*REXX program counts the  occurrences  of a (non─overlapping)  substring  in a string. */
w=.                                                                 /*max. width so far.*/
bag= 'the three truths'    ;      x= "th"       ;        call showResult
bag= 'ababababab'          ;      x= "abab"     ;        call showResult
bag= 'aaaabacad'           ;      x= "aa"       ;        call showResult
bag= 'abaabba*bbaba*bbab'  ;      x= "a*b"      ;        call showResult
bag= 'abaabba*bbaba*bbab'  ;      x= " "        ;        call showResult
bag=                       ;      x= "a"        ;        call showResult
bag=                       ;      x=            ;        call showResult
bag= 'catapultcatalog'     ;      x= "cat"      ;        call showResult
bag= 'aaaaaaaaaaaaaa'      ;      x= "aa"       ;        call showResult
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
countstr:   procedure;  parse arg haystack,needle,start;      if start==''  then start=1
            width=length(needle)
                                  do $=0  until p==0;         p=pos(needle,haystack,start)
                                  start=width + p                    /*prevent overlaps.*/
                                  end   /*$*/
            return $                                                 /*return the count.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
showResult: if w==. then do;                   w=30        /*W:  largest haystack width.*/
                         say center('haystack',w)  center('needle',w%2)  center('count',5)
                         say left('', w, "═")      left('', w%2, "═")    left('', 5, "═")
                         end

            if bag==''  then bag= " (null)"                /*handle displaying of nulls.*/
            if   x==''  then   x= " (null)"                /*   "        "      "   "   */
            say left(bag, w)           left(x, w%2)            center(countstr(bag, x), 5)
            return


  

You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the Wren programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Nial programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Shale programming language
You may also check:How to resolve the algorithm N-queens problem step by step in the Maxima programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the Fortran programming language