Computing.Net > Forums > Unix > check for a palindrome

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

check for a palindrome

Reply to Message Icon

Name: kaustav
Date: April 9, 2002 at 22:15:43 Pacific
Comment:

hi, i need help regarding the following -

1)how to write a shell script in UNIX which would check whether an input string is a palindrome or not.

2) do we have arrays in unix to store numbers and characters ?

thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: PaulS
Date: April 10, 2002 at 08:03:08 Pacific
Reply:

The KornShell supports one-dimensional arrays.


0

Response Number 2
Name: James Boothe
Date: April 10, 2002 at 08:29:40 Pacific
Reply:

A palindrome could have mixed case, punctuation, and spaces, any of which would mess up a reverse-order test, such as:

Are poets a waste? Opera!

To check that, your script would have to downshift or upshift entire string and ignore spaces and punctuation. But your assignment is probably to check if a single lowercase word is a palindrome, such as "racecar".

I know how to reverse the string in one very simple command, but I believe that would defeat the purpose of the assignment, which is to do some array work. The whole word sitting in one array slot is no use, so I would start by getting each letter of the word into separate array slots. Any ideas on how you might break up that word?


0

Response Number 3
Name: Don Arnett
Date: April 13, 2002 at 12:17:59 Pacific
Reply:

The script below takes whatever is on the command line and pulls each character out individually. I haven't used arrays much in Unix, so I'll leave that part to you. I ran this using the bash shell.

text=$@

textLength=`echo $text | wc -c`

i=1

while [ $i -le $textLength ]
do

x=`echo $text | cut -c$i`
echo $x

i=$(( $i + 1 ))

done


0

Response Number 4
Name: Giridhar.N
Date: April 26, 2002 at 05:39:23 Pacific
Reply:

1) write shell programs for the following(in unix)

a) to remove from an ordered array all numbers that occurs
more than once.
b) to check whether a string is a palindrome or not.
c) to sort the given list of numbers using bubble sort.

Pl. send me the above list of programs immdtly anyone.


0
Reply to Message Icon

Related Posts

See More


Question for a Linux/Unix... Compress/Uncompress in UN...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: check for a palindrome

shell script to check for a file on www.computing.net/answers/unix/shell-script-to-check-for-a-file-on/3714.html

Check for new file, exec sql*plus www.computing.net/answers/unix/check-for-new-file-exec-sqlplus/6714.html

scan a folder for a specific file www.computing.net/answers/unix/scan-a-folder-for-a-specific-file/5340.html