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 should look exactly like this and please add comments to the code
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 leteSolution
// Palindrome.java
import java.util.Scanner;
public class Palindrome
{
public static boolean isPalindrome(char[] a, int used)
{
//Convert to string and on remove irrelevant characters in one shot
String s = new String(a).toLowerCase();
int i=0;
int length = used-2;
while(i <= length / 2)
{
if (s.charAt(i++) != s.charAt(length--))
{
//break loop when characters don\'t match
return false;
}
}
return true;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
char again = \'y\';
while(true)
{
System.out.println(\"\ This program will test whatever text your enter to see if it is a Palindrome\ \");
System.out.println(\"Enter text (just letters and blanks with a period at the end):\");
String s = sc.nextLine();
char[] a = s.toCharArray();
if(isPalindrome(a,(a.length)) == true)
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)? \");
again = sc.next().charAt(0);
if(again == \'n\' || again == \'N\')
break;
sc.nextLine(); // Consume newline left-over
}
}
}
/*
output:
This program will test whatever text your enter to see if it is a Palindrome
Enter text (just letters and blanks with a period at the end):
cat.
NO, the phrase is not a Palindrome!
Would you like to test another string (y/n)?
y
This program will test whatever text your enter to see if it is a Palindrome
Enter text (just letters and blanks with a period at the end):
radar.
YES, the phrase is a Palindrome!
Would you like to test another string (y/n)?
y
This program will test whatever text your enter to see if it is a Palindrome
Enter text (just letters and blanks with a period at the end):
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 your enter to see if it is a Palindrome
Enter text (just letters and blanks with a period at the end):
xyzczyx.
YES, the phrase is a Palindrome!
Would you like to test another string (y/n)?
y
This program will test whatever text your enter to see if it is a Palindrome
Enter text (just letters and blanks with a period at the end):
warts n straw.
YES, the phrase is a Palindrome!
Would you like to test another string (y/n)?
n
*/


