Longest common substring: Difference between revisions

→‎{{header|REXX}}: added the REXX language. -- ~~~~
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 123:
<lang J> 'thisisatest' lcs 'testing123testing'
test</lang>
 
=={{header|REXX}}==
<lang rexx>/*REXX program to test the LCSUB (Longest Common Substring) subroutine.*/
parse arg a b . /*get two arguments (strings). */
if a=='' then a= "thisisatest" /*use this string for a default. */
if b=='' then b= "testing123testing" /* " " " " " " */
say ' string A =' a /*echo string A to screen. */
say ' string B =' b /*echo string B to screen. */
say ' LCsubstr =' lcsubstr(a,b) /*tell Longest Common Substring. */
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────LCSUBSTR subroutine─────────────────*/
LCsubstr: procedure; parse arg x,y,$ /*Longest Common Substring. */
#=length(x) /*shortcut for using the X length*/
do j=1 for # /*step through start points in X.*/
do k=# by -1 for # /*step through string lengths. */
_=strip(substr(x,j,k)) /*extract a common substring. */
if pos(_,y)\==0 & length(_)>length($) then $=_ /*longest?*/
end /*j*/ /* [↑] see if it is the longest.*/
end /*k*/
return $ /*$ is null if no common string.*/</lang>
'''output''' when using the default inputs:
<pre>
string A = thisisatest
string B = testing123testing
LCsubstr = test
</pre>