Saturday, February 16, 2013

Java Programming: Palindrome

Description:
A string is a palindrome if it reads the same forward and backward.The words “mom,” “dad,”
and “noon,” for instance, are all palindromes.

Problem:
Write a program that prompts the user to enter a string and reports whether the string is a palindrome.

Solution:
One solution is to check whether the first character in the string is the same as the last character. If so, check whether the second character is the same as the second-to-last character. This process continues until a mismatch is found or all the characters in the string are checked, except for the middle character if the string has an odd number of characters.

To implement this idea, use two variables, say low and high, to denote the position of two characters at the beginning and the end in a string inputString. Initially, low is 0 and high is inputString.length() – 1. If the two characters at these positions match, increment low by 1and decrement high by 1. This process continues until (low >= high) or a mismatch is found.

The solution is in my Github page.

Here's a test run



 

No comments:

Post a Comment