{"id":78,"date":"2011-06-01T16:50:21","date_gmt":"2011-06-01T06:50:21","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=78"},"modified":"2015-11-20T17:49:13","modified_gmt":"2015-11-20T06:49:13","slug":"check-palindromes-using-recursion","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/check-palindromes-using-recursion\/","title":{"rendered":"Check Palindromes using Recursion"},"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

Previously we covered how to check if a String is a Palindrome using a loop to check the characters in the String. An alternative is to use recursion to check the String.<\/p>\n

As with all recursive solutions we need a base case, and rules to reduce all other cases towards the base case.<\/p>\n

For our problem the base case is that any String with less than two characters is a Palindrome.<\/p>\n

To reduce a String containing two or more characters towards our base case can be achieved by stripping off the first and last characters from the String. For the String to be a Palindrome the two characters need to be the same AND the remaining String also needs to be a Palindrome.<\/p>\n

The Java code showing the use of recursion to check for a Palindrome is available for download<\/a> by members<\/a> only. Java mentors are also available to answer any questions you have about the solution.x<\/span><\/div>\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":[4],"tags":[100,7],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-1g","jetpack-related-posts":[{"id":43,"url":"https:\/\/learn-java-by-example.com\/java\/palindromes\/","url_meta":{"origin":78,"position":0},"title":"Palindromes","date":"April 13, 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 "Featured"","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":78,"position":1},"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":251,"url":"https:\/\/learn-java-by-example.com\/java\/add-checkbox-items-jlist\/","url_meta":{"origin":78,"position":2},"title":"How do add a checkbox to items in a JList?","date":"June 30, 2015","format":false,"excerpt":"We often get asked about how to implement a list of checkboxes using Swing. Using a JList filled with JCheckbox's seems the obvious solution, however JList does not support cell editors so this does not work. One possible solutions is to use a single column JTable and store boolean's as\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":78,"position":3},"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":98,"url":"https:\/\/learn-java-by-example.com\/java\/calculator-keypad\/","url_meta":{"origin":78,"position":4},"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":[]},{"id":166,"url":"https:\/\/learn-java-by-example.com\/java\/month-calendar\/","url_meta":{"origin":78,"position":5},"title":"Month Calendar","date":"May 1, 2011","format":false,"excerpt":"The following application shows how to format displaying a month in a way that would be suitable for including in a calendar. This involves making sure days of the week are all vertically aligned, and that the first day of the week appears in the first column. The day that\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/learn-java-by-example.com\/wp-content\/uploads\/2011\/05\/calendar.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/78"}],"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=78"}],"version-history":[{"count":12,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":348,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/78\/revisions\/348"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}