Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Wednesday 22 February 2012

Accept the string and checks whether the string is palindrome or not.


[[   METHOD 1   ]]
clear
echo "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