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.\"

follow this steps. it should recognize a palindrome as a palindrome with and without a period entered.

Create new Scanner
Call method System.out.println with \"This program will test whatever text\"
Call method System.out.println with \"you enter to see if is a palindrome\"
Call method System.out.println with \"(reads the same backwards and forwards).\"
Call method System.out.println
Call method System.out.println with \"Enter text follwed by a period (.)\"
Initialise phrase to keyboard.nextLine
Call method System.out.println
If phrase.length is greater than MAX_CHARS
    Exit: too many characters.
        Call method System.out.println
        Call method System.out.println with \"Too many characters: maximum is 50.\"
    CloseBracket
Else
    If palindrome with phrase
        Call method System.out.println with \"YES, the phrase is a palindrome!\"
    Else
        Call method System.out.println with \"NO, the phrase is NOT a palindrome.\"
    EndIf
EndIf

Next should be the palindrome testing

Parse: remove blanks, save letters in array, & count letters.
Initialise letterCount to 0
Initialise charCount to text.length
Create new char
Char aChar;
i counts through chars in original array
    Set aChar to text.charAt with i
    If not Character.isWhitespace with aChar and ( Character.isLetter with aChar )
        Set letterCount of letter to aChar
        LetterCount++;
    EndIf
EndFor
Test for mirror image (palindrome):
Initialise result to true
For i is 0, i is less than letterCount divided by 2, i increments by 1
    If Character.toUpperCase with position i in letter is not equal to
        Set result to false
    EndIf
EndFor
with result

output should look exactly like this

This program will test whatever text you enter to see if is a palindrome (reads the same backwards and forwards) Enter text (just letters and blanks vith a period at the end, please radar YES, the phrase is a palindrome! Would vou like to test another string (v/n) ? This program will test vhatever text you enter to see if is a palindrome (reads the same backwards and orwards ) Enter text (just letters and blanks with 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 will test vhatever text you enter to see if is a palindrome (reads the same backwards and rwards ) Enter text (just letters and blanks with a period at the end, please | a) le was ere I saw elba. YES. the phrase is a palindrome! Would you like to test another string (y/n) ? GRASP: operation comp lete

Solution

import java.util.Scanner;
import java.lang.Character;

public class Palindrome
{
public static void main(String args[])
{
   int MAX_CHARS=50;
String phrase, reverse = \"\";
Scanner in = new Scanner(System.in);
   System.out.println(\"This program will test whatever text\");
   System.out.println(\"you enter to see if is a palindrome\");
   System.out.println(\"reads the same backwards and forwards\");
  
   Character choice=null;
  
   do{
       phrase = userInput(); // calling input method
       int length = phrase.length();
      
       if(length>50){
           System.out.println();
           System.out.println(\"Too many characters: maximum is 50\");
           //System.exit(0);
           }else{
               for ( int i = length-1; i >= 0; i-- )
                   reverse = reverse + phrase.charAt(i);
               if (phrase.equals(reverse))
                   System.out.println(\"YES, the phrase is a palindrome!.\");
               else
                   System.out.println(\"NO, the phrase is NOT a palindrome.\");
               }
       System.out.println(\"Would you like to test another String(y/n)\");
       choice=in.next().charAt(0);// it takes the first char of user entered choice
       choice=Character.toLowerCase(choice);
       //System.out.println();
   }while(choice!=\'n\');
  
}

public static String userInput()   {
   String phrase;
       System.out.println(\"Enter text follwed by a period (.)\");
       Scanner input = new Scanner(System.in);
       phrase = input.nextLine();
       phrase = phrase.replace(\".\",\"\"); // removing . from string
       return phrase;
  
   }

  
}

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