Search code examples
javapalindrome

A boolean palindrome in Java


So I've been assigned the following problem and I don't even know where to start. Is he wanting the palindrome's pulled from an array? All of the research I've done online about palindrome's, none of them use boolean values. Thanks in advance. Programming isn't my forte to say the least.

Problem 2.1. (5 points) Write a program that accepts a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Disregard blanks when deciding if a string is a palindrome and consider upper and lower case version of the same letter to be equivalent. Provide a static method palindrome

 public static boolean palindrome(char[] a, int number)

that accepts a char array containing the characters of the input string, and an integer defining the number of characters in the string.


Solution

  • What he wants you to do is find out whether the sequence of characters in the array is a palindrome. You don't need to return any information about it. If you are still stuck, here are some tips:

    1. It's a lot easier to work with Strings that char[]s. Try creating a string out of the array and using that in your computations.
    2. It may seem that the number parameter is redundant, but I would be safe and construct the string like this:

      new String(a, 0, number)
      
    3. Now, you should probably filter the string to get rid of uppercase letters and spaces. That gets unnecessary info out of the way.
    4. Now, perhaps remove the middle character of the string, if there is one. That makes the number of chars even.
    5. Now split the string along the middle, reverse one half, and compare the two halves. After that, you know whether it is a palindrome, and you are done.