Shell Script to Check Palindrome


Here’s shell script to check Input string is palindrome or not

len=0
i=1
echo -n “Enter a String: ”
read str
len=`echo $str | wc -c`
len=`expr $len – 1`
halfLen=`expr $len / 2`
while [ $i -le $halfLen ]
do
c1=`echo $str|cut -c $i`
c2=`echo $str|cut -c $len`
if [ $c1 != $c2 ] ; then
echo “string is not palindrome”
exit
fi
i=`expr $i + 1`
len=`expr $len – 1`
done
echo “String is Palindrome”

Algorithm

1) Input string

2)calculate length of string .

3)Run a loop i=0 to len/2 of string

4)compare ith character with len th character

5)if character doesn’t matches within loop then exit saying that its not a plaindrome

6)if loop is successfully executed print that string is palindrome…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s