Java A palindrome is a word or phrase that reads the same fo

Java

A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same letter to be equal.for example,the following are palindromes:

1.warts n straw

2.radar

3.able was I ere I saw Elba

4.xyzczyx

Write a program that will accept a sequence of characters terminated by a period and will dedide whether the string--without the period---is a palindrome.You may assume that the input contains only letters and blanks and is at most 80 characters long.Include a looop that allows the user to check additional strins until she or he requests that the program ind. (Hint Define a static method called isPalindrome that begins as follows:)

/** Precondition: The array a contains letters and blanks in positions a[0] through a[used - 1]. Returns true if the string is a palindrome and false otherwise. */ public static boolean isPalindrome(char[] a, int used)

Your program shoud read the input characters int an arra y whose base type is char and then call the preceding method.the int variable used keeps track of how nuch of the array is used, as described in the section entitled \"Partially Filled Arrays.\"

the output shouls look like this and please add comments to the code

This program vill test whatever text you enter to see if is a palindrome (reads the same backwards and forvards) . Enter text (just letters and blanks vith a period at the end, please. radar. YES, the phrase is a palindrome! Would you like to test another string (y/n)? This program vill test vhatever text you enter to see if is a palindrome (reads the same backvards and forwards) Enter text (just letters and blanks vith a period at the end, please. cat. NO, the phrase is NOT a palindrome Would you like to test another string (y/n) ? This program vill test whatever text you enter to see if is a palindrome (reads the same backvards and forwards) Enter text (just letters and blanks vith a period at the end, please. >| ab 1 e was I ere I saw elba. YES, the phrase is a palindrome! Would you like to test another string (y/n)? -jGRASP: operation complete

Solution

solution

package com.prt.test;

import java.util.Arrays;
import java.util.Scanner;

public class Test {

   public static void main(String[] args)
   {
       testing();
  

   }

   public static void testing() {
       String sentence=\"\";
       String s=\"\";
       Scanner scanner = new Scanner(System.in);
      
       System.out.println(\"enter the string with letters and spaces with period-- or without period\");
      
       String userInput = scanner.nextLine();// to read the string
      
       String[] words=userInput.split(\"-\");//to split the words if user enter the string with period
      
       String singlestring=Arrays.toString(words);//to convert array or strings into single string
      
       String word1 = singlestring.replaceAll(\"\\\\s\", \"\");// to replace the whitespaces
      
       System.out.println(word1);
       //after converting array of strings it stored [cat] format for eliminating angular brackets
   if (word1 != null && word1.length() > 0 && word1.charAt(word1.length()-1)==\']\')//to remove the ] in last
   {
   sentence = word1.substring(0, word1.length()-1);
   s=sentence.substring(1);//to remove [ in first
   // System.out.println(s);
   }

       isPallindrome(s);//forward the control to is pallindrome method

   }

   public static boolean isPallindrome(String word) {
       Scanner scanner = new Scanner(System.in);
       boolean flag = false;

       if (word.length() > 80) {

           System.out.println(\"please enter below 80 characters\");

           testing();
       } else {
           String reverse = \"\";
           int length = word.length();
           for (int i = length - 1; i >= 0; i--) {
               reverse = reverse + word.charAt(i);//it reads the character by character from the end
           }
           System.out.println(reverse);
           if (reverse.equals(word)) {
               System.out.println(\" yes ,the phrase is a pallindrome\");
               retesting();
               return true;
          
           } else {
               System.out.println(\"the phrase is not a pallindrome\");
               retesting();
               return false;
           }
          
       }
       return false;
       }
          
public static void retesting()
{
   boolean flag=false;
Scanner scanner=new Scanner(System.in);
           System.out.println(\"would you like to test another please type only y/n otherwise it terminated\");
           //to read from the user y/n      
           String check = scanner.next();
          
           if (check.equals(\"y\")) {
               flag = true;
               if (flag == true) {

                   testing();
               }
               else
               {
                   System.out.println(\"JGRASP:operation complete\");
                   System.exit(0);
               }
           }

}
       }

  

output

enter the string with letters and spaces with period-- or without period
radar
[radar]
radar
yes ,the phrase is a pallindrome
would you like to test another please type only y/n otherwise it terminated
y
enter the string with letters and spaces with period-- or without period
cat---
[cat]
tac
the phrase is not a pallindrome
would you like to test another please type only y/n otherwise it terminated
y
enter the string with letters and spaces with period-- or without period
straw n warts--
[strawnwarts]
strawnwarts
yes ,the phrase is a pallindrome
would you like to test another please type only y/n otherwise it terminated

n

Java A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same let
Java A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same let
Java A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same let

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site