首页 > 试题广场 >

编 写一个shell 脚本,检查给出的串是否为回文(pali

[问答题]

写一个shell 脚本,检查给出的串是否为回文(palindrome)。

答:


#! /bin/bash




echo "Enter string"

read str

len=`echo $str | wc -c`

len=`expr $len - 1
`

l=`expr $len / 2`

ctr=1

flag=0

while test $ctr -le $l

do

a=`echo $str | cut -c$ctr`

b=`echo $str | cut -c$len`

if test $a -ne $b

then flag=1

break

fi

ctr=`expr $ctr + 1`

len=`expr $len - 1`

done

if test $flag -eq 0

then echo "String is palindrome"

else echo "String not a palindrome"

fi


发表于 2017-05-17 14:50:33 回复(0)