Programming Practice: Palindrome Test

Problem: Detect whether a given string is a palindrome or not. A palindrome is a word that spells the same if it is reversed.

Examples:
Input: “madam”, Output: true
Input: “test”, Output: false
Input: “a”, Output: true
Input: “aabbaa”, Output: true

Algorithm:
1. Initialize two counters, one pointing to the first character, and the second to the last one
2. Iterate over the string. Compare the characters at the two locations. If the characters match then advance the front counter by 1 and the back counter by -1. If the characters don’t match, the string is not a palindrome.
3. If the two counters reach the same location, and everything has matched so far, the string is a palindrome

Java Code:

One Response to Programming Practice: Palindrome Test

  1. Love palindromes. Had a professor in college who plastered his whole office with palindromes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s