24 November 2012

Shell Script to check whether a given String is Palindrome or not

1 comment

Q: How do I check whether a Input String is Palindrome or not in Linux and Unix


Ans:

#!/bin/bash
read -p "Enter the String:" n
len=${#n}
flag=1
for((i=0;i<=len/2;i++))
do
c1="${n:$i:1}"
c2="${n:$len-$i-1:1}"
#comparing single single charcters from begining and end
if [ $c1 != $c2 ];then
flag=0
echo "String is not palindrome"
break
fi
done
if(( $flag==1)); then
echo "Input String is Palindrom"
fi

Output


shell script to check whether string is palindrome or not



If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

1 comment: