Create and test a class Palindrome with a method isPalindrom

Create and test a class Palindrome with a method isPalindrome() that takes a char array as its parameter and determines if the characters in that array represent a palindrome, that is, if they read the same forward and backward, ignoring upper and lower case and any blanks.

------------------------------------------------------------------------------------------------------------------------------------------

The isPalindrome() method header is:
      public static boolean isPalindrome(char[] a)

-------------------------------------------------------------------------------------------------------------------------------------

Write a program that will accept a sequence of characters ending in a period, read by Scanner nextLine(), and will decide whether the String without the period is a palindrome, using the isPalindrome() method. You can assume that the input contains only letters and blanks before the period, and that it does contain a period at the end of the characters to check.

-------------------------------------------------------------------------------------------------------------------------------------

Your main method should contain a loop that allows the user to check as many strings as they want until they type quit.

-------------------------------------------------------------------------------------------------------------------------------------

You may convert the input String to lower case before calling isPalindrome(), or you may have isPalindrome() do the conversion by using static method Character.toLowerCase().

Hints:

Convert the input String to an array of characters before calling isPalindrome() – you can use String method toCharArray(), but only convert the part of the input String before the period:
              
int period = input.indexOf(\".\");
       String toCheck = input.substring(0, period);
       char[] ca = toCheck.toCharArray();

Write a helper method removeBlanks() with this header:
              
private static char[] removeBlanks(char[] a)
that will return a new, possibly shorter, character array with any blanks in a removed. You can use static method
boolean Character.isWhiteSpace(char c) to find blanks in a.

Write another helper method reverse() with this header:
              
private static char[] reverse(char[] a)
that will return a new character array with the original characters in a in reverse order.

Finally, call removeBlanks() in isPalindrome(), then call reverse() and compare the two arrays – if they have all the same (lower case) characters, the input is a palindrome.

Solution

//Tested on eclipse in ubuntu,Linux

//Please make sure input string should ended with dot(.) or input string must contain dot(.)

//otherwise it will throw an exception

/**************Palindrome1.java**************/

import java.util.Scanner;

public class Palindrome1 {
   /* method is used for checking given char array is Palindrome or not */
   public static boolean isPalindrome(char[] a) {
       char[] original;
       original = removeBlanks(a).clone();
       a = reverse(original);

       if (String.valueOf(a).contentEquals(String.valueOf(original))) {
           return true;
       }
       return false;

   }

   /*
   * Removing blank space from char array first converting char array to
   * String Then replace all blank and white spaces from string Then convert
   * back to char array
   */
   private static char[] removeBlanks(char[] a) {
       /* Converting char array to String */
       String line = String.valueOf(a);
       /* Removing Blank spaces from string */
       String lineWithoutSpaces = line.replaceAll(\"\\\\s+\", \"\");
       /* Converting String into char array */
       a = lineWithoutSpaces.toCharArray();
       return a;

   }

   /*
   * Reversing string first convert char array to string Then using String
   * buffer we reversed the string then convert back to char array and
   * returned that char array
   */
   private static char[] reverse(char[] a) {
       /* Converting char array to String */
       String line = String.valueOf(a);
       StringBuffer buffer = new StringBuffer(line);
       buffer.reverse();
       a = buffer.toString().toCharArray();
       return a;

   }

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);
       while (true) {
           /* Prompt for reading input from user */
           System.out.println(\"Please Enter the string\");
           String line = input.nextLine();
           /* getting index of . */
           int period = line.indexOf(\".\");
           /* Getting sub string */
           String toCheck = line.substring(0, period).toLowerCase();
           /* Converting into char array */
           char[] ca = toCheck.toCharArray();

           /* Calling removeBlanks method */

           Boolean flag = Palindrome1.isPalindrome(ca);
           /*
           * If returned flag is true the given input is palindrome otherwise
           * it is not
           */
           if (flag) {
               System.out.println(\"The Input is a Palindrome\");
           } else {
               System.out.println(\"The Input is not a Palindrome\");
           }
           /* Prompt for user input on loop condition */
           System.out.println(\"Do u want to test another string(yes or quit)\");
           String choice = input.nextLine();
           if (choice.equalsIgnoreCase(\"quit\")) {
               System.exit(1);
           }

       }

   }

}


/*************output*******************/

Please Enter the string
radar.
The Input is a Palindrome
Do u want to test another string(yes or quit)
yes
Please Enter the string
malyalam.
The Input is not a Palindrome
Do u want to test another string(yes or quit)
yes
Please Enter the string
malayalam.   
The Input is a Palindrome
Do u want to test another string(yes or quit)
quit


Thanks a lot

Create and test a class Palindrome with a method isPalindrome() that takes a char array as its parameter and determines if the characters in that array represen
Create and test a class Palindrome with a method isPalindrome() that takes a char array as its parameter and determines if the characters in that array represen
Create and test a class Palindrome with a method isPalindrome() that takes a char array as its parameter and determines if the characters in that array represen

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site