Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

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
Read More...

23 November 2012

Shell Script to reverse a String

8 comments

Q: How do I reverse a string in Linux/Unix?


Ans:

#!/bin/bash
read -p "Enter string:" string
len=${#string}
for (( i=$len-1; i>=0; i-- ))
do
# "${string:$i:1}"extract single single character from string.
reverse="$reverse${string:$i:1}"
done
echo "$reverse"
Read More...