Write a shell script to identify the given string is palindrome or not?

Questions by athotaashok   answers by athotaashok

Showing Answers 1 - 68 of 68 Answers

Narayan Singh

  • Feb 21st, 2006
 


print("Please enter a string ");
$input_string = <STDIN>;
chop($input_string);
$rev_string = reverse($input_string);

if($input_string eq $rev_string){
 print("The string is a palindrome");
}else{
 print("The string is NOT a palindrome");
}

  Was this answer useful?  Yes

raj

  • May 13th, 2006
 

this is perl. writing a shell script to do the job is difficult.

  Was this answer useful?  Yes

bwc

  • Jun 9th, 2006
 

Actually you can do this..if [ -z "echo $var | rev | sed -e "s/^$var$//"" ]; then echo pali; else echo nopali; fi

  Was this answer useful?  Yes

Mukund

  • Jul 6th, 2006
 

Hey frnds ,

Cant we write that using control statements like if!!!!

========================================

var=hai

var1=`echo $var|rev`

if [ $var != $var1 ] ; then                                                                                      
echo  $var is not palindrome.                                                                                                                      
else                                                                                                                              
echo  $var is palyndrome 

fi                                                                                                           
                                       

Nonsense

  • Aug 16th, 2006
 

# bash-script: VAR=lagerregalif [[ "$VAR" == `echo $VAR | rev` ]]then echo palindromelse echo no palindromfi

  Was this answer useful?  Yes

vibs

  • Nov 20th, 2006
 

echo -n "Enter number : "
read n

# store single digit
sd=0

# store number in reverse order
rev=""

# store original number
on=$n

while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder
    n=$(( $n / 10 ))  # get next digit
    # store previoues number and current digit in rev
    rev=$( echo ${rev}${sd} )
done

if [ $on -eq $rev ];
then
  echo "Number is palindrome"
else
  echo "Number is NOT palindrome"
fi

  Was this answer useful?  Yes

Kunal

  • Dec 12th, 2006
 

!#/bin/sh

# To check whether the entered number is palindrome or not.

echo "Enter a number: "

read NUM1

echo $NUM1 > file.txt

NUM2=`rev file.txt`

if [ $NUM1 -eq $NUM2 ]; then

echo "Entered Number $NUM1 is a palindrome."

else

echo "Entered Number $NUM1 is not a palindrome."

fi

  Was this answer useful?  Yes

swaroopa

  • Jun 22nd, 2007
 

//Hi, this is for strings

#!/bin/sh

echo "enter a string"
read str

var1=`echo $str|rev`
if [ $var1 != $str ] ; then
echo "no pali"
else
echo "pali"
if

//this is for numbers

#!/bin/sh

echo "Enter a number to find for palindrome"
read num1
num=$num1
i=0

while [ $num -gt 0 ]
do
i=`expr $i * 10 +  $num % 10`
num=`expr $num / 10`
done

if [ $i -eq $num1 ] ; then
echo "$num1 is a palindrome"
else
echo "$num1 is not a palindrome"
f

  Was this answer useful?  Yes

sudhir

  • Sep 15th, 2007
 

can u please  explain the statement rev=$(echo ${f}${f1}) please.
I did  not understand  how its working.

  Was this answer useful?  Yes

Vivek12

  • Jun 4th, 2008
 

echo enter a string
read string
len=`expr length "$string"`
i=$len
reverse=''
while [ $i -gt 0 ]
do
char=`echo $string | cut -c $i`
reverse="$reverse$char"
i=`expr $i - 1`
done
echo string:$string
echo reverse:$reverse
if [ $reverse = $string ]
then
echo its a palindrome
else
echo its not a palindrome
fi

Hope this helps :)

  Was this answer useful?  Yes

Shanthi

  • Jun 26th, 2014
 

st1="abc"
for (( i=$len-1; i>=0; i-- ))
do
reve="$reve${st1:$i:1}"
done

  Was this answer useful?  Yes

Tigran Petrosyan

  • Oct 23rd, 2014
 

Code
  1. string="asasa"

  2. for ((i=0; i<${#string}/2; i++))

  3. {

  4.   start=${string:$i:1}

  5.   end=${string:${#string}-$i-1:1}

  6.   if [[ $start != $end ]]; then

  7.     echo "no palindrome"

  8.     exit 1

  9.   fi

  10. }

  11. echo "palindrome"

  12. exit 0

  Was this answer useful?  Yes

Mohit

  • Nov 14th, 2014
 

Code
  1. echo "$string" >> /temp/file.tmp

  2.  

  3. rev_str=`rev /temp/file.tmp`;

  4.  

  5.  if [[ "$rev_str" == "$string" ]]

  6.         then

  7.         echo palindrome

  8.         else

  9.         echo "non-palindrome"

  10. fi

  Was this answer useful?  Yes

yogesh Gavhane

  • Oct 27th, 2015
 

#!/bin/bash
read -p "enter a string::" s1
echo $s1 > /tmp/s1.txt
s2=`rev /tmp/s1.txt`
if [ $s1 = $s2 ]; then
echo "your string is palindrome"
else
echo "not a palindrome"
fi

  Was this answer useful?  Yes

Ather Ahmed

  • Nov 9th, 2015
 

#!/bin/bash
echo -n "Enter the string : "
read string
checkIfPalindrome() {
if [ $(echo "$1") != $(echo "$1" | rev) ]
then echo "$string is not a palindrome"
else echo "$string is a palindrome"
fi
}
actualstring=$(echo "$string" | sed s/[ ]*//g)
check=$(echo "$actualstring" | egrep -i ^[a-z]*$ | wc -w)
if [ $check != 0 ]
then checkIfPalindrome $actualstring
else echo "The value entered is not a string!"
exit
fi

  Was this answer useful?  Yes

Ravi

  • Mar 4th, 2016
 

#!/bin/bash
text=$1;
len=`echo "$text"|awk {print length}`
j=$(($len / 2));
for ((i=1,k="$len";i<="$j";i++))
do
if [ `echo $1|cut -c"$i"` = `echo $1|cut -c"$k"` ]
then
k=$((k - 1))
continue
else
echo "not palindrome"
exit
fi
done
echo "palindrome"

  Was this answer useful?  Yes

amita

  • Apr 1st, 2016
 

#!/bin/bash
echo "Enter Number"
read number
number=$1
number1="echo $1 | rev"
if [ $number -eq $number1 ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi

  Was this answer useful?  Yes

pratap reddy

  • Apr 18th, 2016
 

#!/bin/bash
read -p "Enter string:" string
len=${#string}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${string:$i:1}"
done
echo "$reverse"
if [ $reverse = $string ]
then
echo " palindrome "
else
echo " not palindrome "
fi

  Was this answer useful?  Yes

shubham garg

  • Apr 20th, 2016
 

#!/bin/bash
echo enter the number
read a
b=`echo $a|rev`
test "$a" == $b && echo your number is palindromic || echo your number is not palindromic

Code
  1. #!/bin/perl

  2. print "enter the number

  3. ";

  4. chomp($a=<stdin>);

  5. $ab=reverse("$a");

  6. if ( "$a" eq "$ab" )

  7. {

  8. print "your number is palindromic

  9. ";

  10. }

  11. else

  12. {

  13. print "your number is not palindromic

  14. ";

  15. }

  Was this answer useful?  Yes

Richa

  • May 21st, 2016
 

#!/bin/sh
read -p "Enter the string" str
str_new=$str
reverse_str=`echo "$str" | rev`
if [ $str_new == $reverse_str ]; then
echo "Palindrome"
else
echo "Not a palindrome"
fi

  Was this answer useful?  Yes

uma

  • May 24th, 2016
 

#!/bin/sh
read -p "Enter the string" str
str_new=$str
reverse_str=`echo "$str" | rev`
if [ $str_new = $reverse_str ]
then
echo "Palindrome"
else
echo "Not a palindrome"
fi

  Was this answer useful?  Yes

raghwendra ranbir

  • Aug 18th, 2016
 

#!/bin/bash
read -p "please enter the string" string
length=${#string}
while [ $length -gt 0 ]
do
rev_str=revs_str`echo $string|cut -c$length`
length=$length -1
done
echo "$rev_str"

  Was this answer useful?  Yes

Mohsin Khan

  • Jan 6th, 2017
 

#!/bin/sh
echo "please enter the string"
read string
length=${#string}
while [ $length -gt 0 ]
do
rev_str=$rev_str`echo $string|cut -c$length`
length=$length -1
done
echo "$rev_str"
if [ $string = $rev_str ]
then
echo "Palindrome"
else
echo "Not a palindrome"
fi

  Was this answer useful?  Yes

sujit kumar

  • May 4th, 2017
 

#!/bin/bash
echo -n "Enter a string: "
read str
if [ $(echo $str | rev) = $str ]
then
echo "$str is palindrome."
else
echo "$str is not palindrome"

  Was this answer useful?  Yes

armaan mahajan

  • May 21st, 2017
 

#!/bin/bash
echo "please enter the string"
read string
string_new=$string
reverse_string=`echo $string|rev`
if [ $reverse_string -eq $string_new ];
then
echo "String is palindrome"
else
echo "string is not palindrome"
fi

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions