Question

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.

Was it helpful?

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.

OTHER TIPS

I assume that you know what is a palindrome. The method that they ask you to write is a palindrome detector, with some advanced features. You are not constructing a palindrome: instead, you detect if a string that they give you is a palindrome or not.

The algorithm is straightforward: you need two indexes - one stars at zero, the other one starts at number-1 (recall that Java arrays are zero-based). Find the next non-space character at each side, convert to uppercase, and compare. If the characters are different, return false. Otherwise advance the two indexes toward each other. Once they cross in the middle, return true.

Note: For some reason they are passing the length of the array on the side. This would make sense if it were an assignment in C, but in Java it makes zero sense, because the array can report its length to you.

I assumed you are allowed to use full range of the Java API, in this case Arrays. This would have been done easier using Strings, but since the requirement is to use arrays, there you go:

public static boolean isPalindrome(char[] input, int length)
{    
    // Remember the original array
    char[] original = Arrays.copyOf(input, length);

    char temp;

    // Reverse the array. This could be optimised, it's your call
    for (int i = 0; i < length / 2; i++) 
    {
        temp = input[i];
        input[i] = input[length - 1 - i];
        input[length - 1 - i] = temp;
    }

    // Use the Arrays helper again, to check for equality
    // BE CAREFUL: This is not the same as original.equals(input) !!!
    return Arrays.equals(original, input);
}

You pull the palindrome from an array of characters. Your method should presumably accept an array of characters less than 30 in length. Then you must determine if the first n-1 characters form a palindrome.

A good place to start would be to loop over the length of the array comparing values, make sure to remember that 'A' should equal to 'a'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top