Longest palindromic substrings: Difference between revisions

m
→‎{{header|REXX}}: shortened a long statement (line), add/changed comments and whitespace.
m (added related tasks.)
m (→‎{{header|REXX}}: shortened a long statement (line), add/changed comments and whitespace.)
Line 733:
<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.*/
if s=='' | s=="," then s= 'babaccd rotator reverse forever several palindrome abaracadaraba'
/* [↑] the case of strings is respected*/
do i=1 for words(s); say; x= word(s, say i) /*searchobtain eacha ofstring theto be (S)examined. strings. */
xL= wordlength(s, ix); L m= length(x)0 /*get athe string's tolength; beSet examinedmax & lengthlen.*/
do LL=2 for L-1 /*start with palindromes of length two.*/
m= 0
if do LL=2 for L-find(1) then m= max(m, LL) /*Found a palindrome? Set /*start with palindromes ofM=new length two.*/
end if find(1) then m= max(m,LL) /*Found a palindrome? Set M=new lengthLL*/
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)
say ' do n (length=1'LL") for" words word(@, n) /*display a " /*show longest palindromic substringssubstring. */
end /*n*/; say; ' say (length='LL") " /*display word(@,a n)two─blank separation fence.*/
end end /*ni*/
end /*i*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
find: parse arg short /*if SHORT==1, only find 1 palindrome.*/
@= /*initialize palindrome list to a null.*/
do j=1 for L-LL+1; $= substr(x, j, LL) /*obtain length ofa possible palindromespalindromic substr.*/
if $\= substr=reverse(x,$) j, LL)then iterate /*Not a palindrome? /*obtain a possibleThen palindromicskip substrit.*/
@= @ $ if $\==reverse($) then iterate /*Not a palindrome? Then skip it. /*add a palindromic substring to a list*/
if short then @=return @1 $ /*we have found one palindrome. /*add a palindromic substring to a list*/
end /*j*/; return 0 if short==1 then return 1 /*have found" one " palindrome. " some palindrome(s). */</lang>
end /*j*/; return 0 /* " " some palindrome(s). */</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>