{"id":43,"date":"2011-04-13T20:47:35","date_gmt":"2011-04-13T10:47:35","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=43"},"modified":"2011-07-02T00:42:08","modified_gmt":"2011-07-01T14:42:08","slug":"palindromes","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/palindromes\/","title":{"rendered":"Palindromes"},"content":{"rendered":"
\n

”<\/span> A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction “<\/span><\/p>\n

Palindrome, Wikipedia<\/a><\/cite><\/p><\/blockquote>\n

A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. <\/p>\n

The way to determine this is to compare the characters on the left and right ends of the String. If they don’t match then its not a Palindrome. If they match then we need to compare the next two most inner characters. We continue this until we find a pair that don’t match or we reach the ‘middle’ of the String. If we reach the middle then we have a Palindrome.<\/p>\n

This can be done using either a loop or recursion. The following code shows how it can be achieved using a loop.<\/p>\n

\r\npublic class Palindrome {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tString[] input = {\r\n\t\t\t"abcdefg",\r\n\t\t\t"abccba",\r\n\t\t\t"abcba",\r\n\t\t\t"hannah",\r\n\t\t\t"array",\r\n\t\t\t"java"\r\n\t\t};\r\n\t\t\r\n\t\t\/\/ Loop through an array of Strings,\r\n\t\t\/\/ testing if each is a Palindrome or not\r\n\t\t\r\n\t\tfor (int i=0; i<input.length; i++) {\r\n\t\t\t\r\n\t\t\t\/\/ Test if next string is a palindrome\r\n\t\t\t\r\n\t\t\tboolean palindrome = isPalindrome(input[i]);\r\n\t\t\tif (palindrome) {\r\n\t\t\t\tSystem.out.println(input[i]+" is a palindrome");\r\n\t\t\t} else {\r\n\t\t\t\tSystem.out.println(input[i]+" is not a palindrome");\t\t\t\t\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t\r\n\t\/**\r\n\t * Test whether a given String is a Palindrome or not\r\n\t * @param s the String to test\r\n\t * @return true is String is a palindrome, false otherwise\r\n\t *\/\r\n\t\r\n\tpublic static boolean isPalindrome(String s) {\r\n\t\t\r\n\t\t\/\/ Start two indexes\r\n\t\t\/\/ One from left side of string\r\n\t\t\r\n\t\tint left = 0;\r\n\t\t\r\n\t\t\/\/ And the other from right side\r\n\t\t\r\n\t\tint right = s.length() - 1;\r\n\t\t\r\n\t\t\/\/ Now compare characters from the outside in\r\n\t\t\/\/. Once we get to the middle we can stop\r\n\t\t\r\n\t\twhile (left<right) {\r\n\t\t\t\r\n\t\t\t\/\/ Compare characters\r\n\t\t\t\r\n\t\t\tif (s.charAt(left)!=s.charAt(right)) {\r\n\t\t\t\t\r\n\t\t\t\t\/\/ They are different so its not a palindrome\r\n\t\t\t\t\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/ test the next characters in\r\n\t\t\t\r\n\t\t\tleft++;\r\n\t\t\tright--;\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ All the characters matched so it must be a palindrome\r\n\t\t\r\n\t\treturn true;\r\n\t}\r\n}\r\n<\/pre>\n

We’ll cover a recursive solution to the same problem in a future post.<\/p>\n","protected":false},"excerpt":{"rendered":"

A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. The way to determine this is to compare the characters on the left and right ends of the String. If they…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[24,4],"tags":[6,7],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-H","jetpack-related-posts":[{"id":78,"url":"https:\/\/learn-java-by-example.com\/java\/check-palindromes-using-recursion\/","url_meta":{"origin":43,"position":0},"title":"Check Palindromes using Recursion","date":"June 1, 2011","format":false,"excerpt":"A popular problem is how to determine if a String is a Palindrome or not, the following will also assume we are only dealing with Palindromes of single words ie. no spaces or punctuation. The way to determine this is to compare the characters on the left and right ends\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":154,"url":"https:\/\/learn-java-by-example.com\/java\/prime-numbers\/","url_meta":{"origin":43,"position":1},"title":"Prime numbers","date":"January 21, 2011","format":false,"excerpt":"The assignment here is to calculate all prime numbers less than 100. The solution provided uses the following to determine if a given number is prime. 2 is prime Any number divisible by 2 is not prime If the number is divisible by any odd number then it is not\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":235,"url":"https:\/\/learn-java-by-example.com\/java\/check-java-string-integer\/","url_meta":{"origin":43,"position":2},"title":"How to check if a Java String is an integer?","date":"June 5, 2015","format":false,"excerpt":"The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":238,"url":"https:\/\/learn-java-by-example.com\/java\/selection-sort\/","url_meta":{"origin":43,"position":3},"title":"Selection Sort","date":"June 19, 2015","format":false,"excerpt":"When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It's not a particularly efficient algorithm and really only suitable for\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":175,"url":"https:\/\/learn-java-by-example.com\/java\/money-change-breakdown\/","url_meta":{"origin":43,"position":4},"title":"Money Change Breakdown","date":"March 28, 2011","format":false,"excerpt":"What is the smallest number of coins (or notes) that are required to give a specified amount of change? This is the next problem we will address. To solve this problem we use basically the same process a shop keeper would when giving change to customers. That is we look\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":98,"url":"https:\/\/learn-java-by-example.com\/java\/calculator-keypad\/","url_meta":{"origin":43,"position":5},"title":"Calculator Keypad","date":"August 14, 2010","format":false,"excerpt":"This problem involves understanding some of the basics of building a Swing application including how individual components are laid out inside a window, and how your application can react to the user interacting with the application. In this case we need to update a text field whenever a button is\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/43"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":9,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":125,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/43\/revisions\/125"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}