answersLogoWhite

0


Best Answer

When working with letters we must work with strings (character arrays). To determine if a string is a palindrome we use two pointers, one for each end of the string. So long as the characters pointed to are the same, we advance the pointers one character inwards. If the characters pointed at differ, the string is not a palindrome and we can stop checking. When the pointers meet or pass each other, the string must be a palindrome so we can stop checking.

Note that the string must be composed entirely of letters of the same case (typically lower-case). All punctuation and word-spacing must also be removed since we're only interested in the letters. We achieve this by copying the string and manipulating the copy, leaving the original string intact.

When working with integers, we simply reverse the digits and test to see if it is equal to the original integer. We achieve this by repeatedly dividing the integer by 10 and taking the remainder to extract each digit:

Algorithm: reverse_integer

Input: a positive integer, n

Output: the integer reversed

r = 0

repeat while n > 0

m = n % 10 // % = modulo (remainder after division by 10)

r = r * 10 + m

n = n / 10

end repeat

return r

A less efficient method is to convert the number to a string and then test the string.

A real number is a palindrome when there are as many digits before the decimal point as there are after it, and the integer component is the reverse of the fractional component. We can test this by taking the integer component and reversing it using the algorithm above. We then convert back to a real number and divide by 10 until the number is less than 1. We than add on the original integer component and compare with the original real number. If they are equal, the real number is a palindrome.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are some examples of palindrome programs using letters and numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp