An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Thursday, December 4, 2008

Shell Scripting #5 - Palindrome Script

No comments :
Keywords: Linux, Shell Scripting, Scripts, BASH, Palindrome

Following code snippet is to find whether the given string is palindrome or not:

#!/bin/sh

echo -n "Enter a String : "
read string

original=$string

length=${#string}

reversed=""
for (( i=$length ; i>0 ; i-- ))
do
        reversed=$reversed""${string:$i-1:$i}
        string=${string%${string:$i-1:$i}}
done

string=$original

#reversed=`echo $string | rev`

if test $string = $reversed
then
        echo "Valid Palindrome String"
else
        echo "Invalid Palindrome String"
fi

Keywords: Linux, Shell Scripting, Scripts, BASH, Palindrome

No comments :