Java programe A palindrome is a word or phrase that reads th
Java programe
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.\"
please follow these protocol
set maximum number of characters
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 (just leters and blanks, please.\"
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
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
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
your output should look exactly like this
Solution
//Tested on eclipse
import java.util.Scanner;
public class Palindrome {
private static int MAX_CHARS = 80;
public static boolean isPalindrome(char[] a, int used) {
int k = 0;
/*
* checking given string is palindrome otr not checking first character
* and last character are same or not same for all character
*/
for (int i = used - 1; i >= 0; i--) {
if (a[i] != a[k]) {
return false;
}
k++;
}
return true;
}
public static void main(String args[]) {
/* Scanner class for taking input */
Scanner input = new Scanner(System.in);
String phrase;
while (true) {
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).\");
System.out.println(\"Enter text (just leters and blanks with a period at the end, please.\");
phrase = input.nextLine();
if (phrase.length() > Palindrome.MAX_CHARS) {
System.out.println(\"Too many characters: maximum is 80.\");
} else {
char character[] = phrase.toLowerCase().toCharArray();
Boolean flag = Palindrome.isPalindrome(character, character.length);
if (flag) {
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)?\");
String choice = input.next();
input.nextLine();
if (choice.equalsIgnoreCase(\"y\")) {
} else {
System.exit(1);
}
}
}
}
}
/*******************output*************/
This program will test whatever text
you enter to see if is a palindrome
(reads the same backwards and forwards).
Enter text (just leters and blanks with a period at the end, please.
warts n straw
YES, the phrase is a palindrome
Would you like to test another String (y/n)?
y
This program will test whatever text
you enter to see if is a palindrome
(reads the same backwards and forwards).
Enter text (just leters and blanks with a period at the end, please.
radar
YES, the phrase is a palindrome
Would you like to test another String (y/n)?
y
This program will test whatever text
you enter to see if is a palindrome
(reads the same backwards and forwards).
Enter text (just leters and blanks with a period at the end, please.
able was I ere I saw Elba
YES, the phrase is a palindrome
Would you like to test another String (y/n)?
y
This program will test whatever text
you enter to see if is a palindrome
(reads the same backwards and forwards).
Enter text (just leters and blanks with a period at the end, please.
xyzczyx
YES, the phrase is a palindrome
Would you like to test another String (y/n)?
n
Thanks a lot


