Longest palindromic substrings: Difference between revisions

Content added Content deleted
m (added related tasks.)
m (→‎{{header|REXX}}: shortened a long statement (line), add/changed comments and whitespace.)
Line 733: Line 733:
<lang rexx>/*REXX program finds and displays the longest palindromic string(s) in a given string. */
<lang rexx>/*REXX program finds and displays the longest palindromic string(s) in a given string. */
parse arg s /*obtain optional argument from the CL.*/
parse arg s /*obtain optional argument from the CL.*/
if s=='' | s=="," then s= 'babaccd rotator reverse forever several palindrome abaracadaraba'
if s==''|s=="," then s='babaccd rotator reverse forever several palindrome abaracadaraba'
/* [↑] the case of strings is respected*/
/* [↑] the case of strings is respected*/
do i=1 for words(s); say; say /*search each of the (S) strings. */
do i=1 for words(s); x= word(s, i) /*obtain a string to be examined. */
x= word(s, i); L= length(x) /*get a string to be examined & length.*/
L= length(x); m= 0 /*get the string's length; Set max len.*/
do LL=2 for L-1 /*start with palindromes of length two.*/
m= 0
do LL=2 for L-1 /*start with palindromes of length two.*/
if find(1) then m= max(m, LL) /*Found a palindrome? Set M=new length*/
if find(1) then m= max(m,LL) /*Found a palindrome? Set M=new length*/
end /*LL*/
LL= max(1, m)
end /*LL*/
call find 0 /*find all palindromes with length LL.*/
LL= max(1,m)
say ' longest palindromic substrings for string: ' x
call find .
say '────────────────────────────────────────────'copies('─', 2 + L)
say ' longest palindromic substrings for string: ' x
do n=1 for words(@) /*show longest palindromic substrings. */
say '────────────────────────────────────────────'copies('─', 2 + L)
do n=1 for words(@) /*show longest palindromic substrings. */
say ' (length='LL") " word(@, n) /*display a " substring. */
say ' (length='LL") " word(@, n)
end /*n*/; say; say /*display a two─blank separation fence.*/
end /*n*/
end /*i*/
end /*i*/
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
find: parse arg short /*if SHORT==1, only find 1 palindrome.*/
find: parse arg short /*if SHORT==1, only find 1 palindrome.*/
@= /*initialize palindrome list to a null.*/
@= /*initialize palindrome list to a null.*/
do j=1 for L-LL+1 /*obtain length of possible palindromes*/
do j=1 for L-LL+1; $= substr(x, j, LL) /*obtain a possible palindromic substr.*/
$= substr(x, j, LL) /*obtain a possible palindromic substr.*/
if $\==reverse($) then iterate /*Not a palindrome? Then skip it.*/
if $\==reverse($) then iterate /*Not a palindrome? Then skip it.*/
@= @ $ /*add a palindromic substring to a list*/
@= @ $ /*add a palindromic substring to a list*/
if short then return 1 /*we have found one palindrome. */
if short==1 then return 1 /*have found one palindrome. */
end /*j*/; return 0 /* " " " some palindrome(s). */</lang>
end /*j*/; return 0 /* " " some palindrome(s). */</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>