Manual Reference Pages  - longest_common_substring (3m_strings)

NAME

longest_common_substring(3f) - [M_strings:COMPARE] function that returns the longest common substring of two strings.

CONTENTS

Synopsis
Description
Options
Returns
Examples

SYNOPSIS

function longest_common_substring(a,b) result(match)

    character(len=*),intent(in)  :: a, b
    character(len=:),allocatable :: match

DESCRIPTION

function that returns the longest common substring of two strings.

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".

OPTIONS

a,b strings to search for the longest common substring.

RETURNS

longest_common_substring
  the longest common substring found

EXAMPLES

Sample program

   program demo_longest_common_substring
   use M_strings, only : longest_common_substring
   implicit none
     call compare(’testing123testingthing’,’thisis’,             ’thi’)
     call compare(’testing’,             ’sting’,              ’sting’)
     call compare(’thisisatest_stinger’,’testing123testingthing’,’sting’)
     call compare(’thisisatest_stinger’, ’thisis’,            ’thisis’)
     call compare(’thisisatest’,         ’testing123testing’,   ’test’)
     call compare(’thisisatest’,      ’thisisatest’,     ’thisisatest’)
   contains

subroutine compare(a,b,answer) character(len=*),intent(in) :: a, b, answer character(len=:),allocatable :: match character(len=*),parameter :: g=’(*(g0))’ match=longest_common_substring(a,b) write(*,g) ’comparing "’,a,’" and "’,b,’"’ write(*,g) merge(’(PASSED) "’,’(FAILED) "’,answer == match), & & match,’"; expected "’,answer,’"’ end subroutine compare

end program demo_longest_common_substring

expected output

   comparing "testing123testingthing" and "thisis"
   (PASSED) "thi"; expected "thi"
   comparing "testing" and "sting"
   (PASSED) "sting"; expected "sting"
   comparing "thisisatest_stinger" and "testing123testingthing"
   (PASSED) "sting"; expected "sting"
   comparing "thisisatest_stinger" and "thisis"
   (PASSED) "thisis"; expected "thisis"
   comparing "thisisatest" and "testing123testing"
   (PASSED) "test"; expected "test"
   comparing "thisisatest" and "thisisatest"
   (PASSED) "thisisatest"; expected "thisisatest"


Nemo Release 3.1 longest_common_substring (3m_strings) July 20, 2024
Generated by manServer 1.08 from 08308c8b-fea3-4aa2-b36d-7cdabd4688f9 using man macros.