forked from manwar/perlweeklychallenge-club
/
ch-2.sh
53 lines (45 loc) · 866 Bytes
/
ch-2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/sh
#
# See ../README.md
#
#
# Run as: bash ch-2.sh < input-file
#
set -f
declare -A palindromes
function is_palindrome () {
local string=$1
if ((${#string} < 2))
then
is_palindrome=1
return
fi
local f=${string:0:1}
local s=${string: -1:1}
if [ $f != $s ]
then is_palindrome=0
return
fi
is_palindrome ${string:1:$((${#string} - 2))}
}
while read line
do palindromes=()
for ((i = 0; i < ${#line}; i ++))
do for ((j = i; j < ${#line}; j ++))
do sub=${line:$i:$((j-i+1))}
is_palindrome $sub
if (($is_palindrome == 1))
then palindromes[$sub]=1
fi
done
done
first=1
for p in "${!palindromes[@]}"
do if ((first == 1))
then ((first ++))
else printf " "
fi
printf %s $p
done
echo
done