[[ METHOD 1 ]]
clearecho "Enter string \c"
read str
len=`echo $str|wc -c`
echo "length"$len
i=1
while [ $i -le `expr $len / 2` ]
do
char1=`echo $str|cut -c $i`
temp=`expr $len - $i`
char2=`echo $str|cut -c $temp`
i=`expr $i + 1`
if [ "$char1" != "$char2" ]; then
i=10;
fi
done
if [ $i -eq 10 ]; then
echo "string is not palindrome"
else
echo "string is palindrome"
fi
[[ METHOD 2 ]]
echo "Enter string \c"
read str
len=`echo $str|wc -c`
len=`expr $len - 1`
echo "length"$len
i=1
while [ $i -le $len ]
do
revstr=`echo $str|cut -c$i`$revstr
i=`expr $i + 1`
done
if [ "$revstr" == "$str" ];then
echo "string is palindrome"
else
echo "string is not palindrome"
fi
No comments:
Post a Comment