Palindrometer Program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    \While driving the other day, John looked down at his odometer, and it read 100000. John
    was pretty excited about that. But, just one mile further, the odometer read 100001, and
    John was REALLY excited! You see, John loves palindromes (things that read the same
    way forwards and backwards. So, given any odometer reading, what is the least number
    of miles John must drive before the odometer reading is a palindrome? For John, every
    odometer digit counts. If the odometer reading was 000121, he wouldn't consider that a
    palindrome."
    Write a script that solves this problem. The script is invoked with the current odometer
    reading on the command line, and it should echo back the total number of miles to drive
    to get the next reading that is a palindrome. For example when invoked as
    should echo back
    1
    and
    palindrometer 100000
    palindrometer 000121
    should echo back
  1. Relevant commands, code, scripts, algorithms:
    I couldn't understand how to make this program, I tried my best to understand the program but I couldn't it.

  2. The attempts at a solution (include all code and scripts):
    Tried with 6 digit input and in next palindrome subtracted it

  3. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Narasaraopeta Engineering College, Guntur (Andhra Pradesh) INDIA.
    Professor: Sudeepthi, Code No: Q0501/R05 (Unix and Shell Programming)

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Still, what did you try? It'd also be good to know what shell we're supposed to use!

I used Borne Shell

"What variety is your shell?" "Bourne"
"What model is your car?" "Blue"

Both very vague answers. If I wrote an answer for you in my shell odds are good it wouldn't work in your shell.

If you don't know what your shell is, do you know what your system is?

While Corona is absolutely correct in stating that you should document your environment and what you tried so far (btw: it is good style in IT to document thoroughly! You can't start too soon to develop good habits!) i'd like to tackle a different part of your problem meanwhile:

How would you solve the problem of finding the next palindrome number manually? Which mathematical algorithm would you use?

I hope this helps.

bakunin

I agree with the above posters. We need to know precisely what Operating System you have and exactly what Shell you have. Nowadays it is quite rare to encounter a raw Bourne Shell.
The palindrone question is an advanced text manipulation exercise which usually requires you to first determine the length of the string, then reverse the order of the characters in the string, and then compare the before and after string. If they are the same then it is a palindrome.
There are some extraordinarily complex methods for doing this involving arrays however it may be worth checking out the unix "rev" command if this command is allowed by your tutor.

This example script should work with any true Bourne Shell.

string="abba"
string_rev="`echo "${string}"|rev`"
if [ "${string}" = "${string_rev}" ]
then
    echo "Palindrome: ${string}"
fi

Ps. The straightforward answer to the original question is "zero". I do hope that this isn't an interview question.
However if you continue from 100001 and attempt to find the next palindrone (which I guess is 101101) by incremental methods then this is much more interesting.

Conforming to old Bourne Shell syntax we arrive at something like this:

counter="100001"
while true
do
        counter=`expr ${counter} + 1`
        reverse=`echo "${counter}"|rev`
        echo "${counter} ${reverse}"
        if [ "${counter}" = "${reverse}" ]
        then
                echo "Palindrone: ${counter} ${reverse}"
                break
        fi
done
# Deliberately ignored the requirement to return either 1 or 979 but this is trivial.
# Also deliberately avoided the requirement for the initial mileage to be a parameter. The core code is to find out the next palindrone

If your course context suggests that the string reversal bit should be done with string arrays, then it is up to you to how you reverse the string.