How to resolve the algorithm Longest common substring step by step in the BaCon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Longest common substring step by step in the BaCon programming language

Table of Contents

Problem Statement

Write a function that returns the longest common substring of two strings. Use it within a program that demonstrates sample output from the function, which will consist of the longest common substring between "thisisatest" and "testing123testing". Note that substrings are consecutive characters within a string.   This distinguishes them from subsequences, which is any sequence of characters within a string, even if there are extraneous characters in between them. Hence, the longest common subsequence between "thisisatest" and "testing123testing" is "tsitest", whereas the longest common substring is just "test".

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Longest common substring step by step in the BaCon programming language

Source code in the bacon programming language

FUNCTION Common_Sub$(haystack$, needle$)

    WHILE LEN(needle$)
        FOR x = LEN(needle$) DOWNTO 1
            IF INSTR(haystack$, LEFT$(needle$, x)) THEN RETURN LEFT$(needle$, x)
        NEXT
        needle$ = MID$(needle$, 2)
    WEND
    EXIT

ENDFUNC

PRINT Common_Sub$("thisisatest", "testing123testing")

  

You may also check:How to resolve the algorithm Higher-order functions step by step in the Sidef programming language
You may also check:How to resolve the algorithm Statistics/Normal distribution step by step in the Java programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the COBOL programming language
You may also check:How to resolve the algorithm Cistercian numerals step by step in the Raku programming language
You may also check:How to resolve the algorithm Solve a Holy Knight's tour step by step in the Tcl programming language