• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Recursive method  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I am trying to solve a following problem : Write a recursive function that determines whether an array is a palindrome, where the array and its size are given as parameters. The method returns 1 if a[] is a palindrome, 0 otherwise. The exercise comes from : http://www.bowdoin.edu/~ltoma/teaching/cs107/spring05/recursion.html

I've got a solution that seems to work,but since recursion is difficult to me and I am self-studying, it would be great to get some feedback on it. Here it is :




 
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also need to clean up your indentation.
 
Carey Brown
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only need to test half the characters against the other half. No need to count from length N to zero.
 
Marshal
Posts: 64172
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I it works, it can't be that bad, but, don't use ints instead of booleans. Try the following inputIt is slightly odd the way that your exercise requires the length of the array, because every array has a field called length, Maybe the exercise is intended to be solved in C, not Java®, because:-
  • 1: The [] are written later than would be correct style in Java®.
  • 2: The size of the array would be required in C, but isn't in Java®.
  • 3: Java® has a boolean type, but C doesn't and has to use int.
  • The method names aren't correct style for either language: in C they would read is_palindrome() and in Java® isPalindrome(). Don't call classes things like MyClass or C1. Give them informative names.
    So let's try the method according to Java® conventions:-Note I have called the second parameter pairLocation because it doesn't determine the size of the array, but which pair of letters you are comparing. You are right to use the subtractions, but you need to work out where to stop. There is no need to go back to pairLocation = 0.
    What are the criteria for being a palindrome:-
  • 1: A String with one letter or less is a palindrome.
  • 2: A String is a palindrome if its first and last letters are the same, and the remainder with those two letters taken off is a palindrome.
  • Use the :? operator or a singlle expression if possible.Your lines 24‑26 are nearly as incorrect as the last code block I showed you. Look at our style suggestions whch will give you a hint about what is wrong.
     
    Bartender
    Posts: 2230
    94
    Eclipse IDE Google Web Toolkit Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Recursively, my mind thinks in the following way :
     
    Sheriff
    Posts: 5931
    155
    Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And keep in mind that it has to work on palindromes such as "redder" that have an even number of letters.
     
    Campbell Ritchie
    Marshal
    Posts: 64172
    215
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OP: What progress have you made. Did you find out what happens if you use new char[]{'c', 'h', 'r', 'o', 'n', 'i', 'c'} as input?
     
    look! it's a bird! it's a plane! It's .... a teeny tiny ad
    Create Edit Print & Convert PDF Using Free API with Java
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!