A palindrome is any word, phrase, or sentence that reads the same forward and ba
ID: 3632586 • Letter: A
Question
A palindrome is any word, phrase, or sentence that reads the same forward and backward.
Create a PalindromeTester Program so that the spaces and punctuations are not considered when determining whether a string is a palindrome.The program must ask user to input the word. The program must stop if user provides a black line. (example of program running below). This involves loops, nested loops.
------------------------------
The following are sample interactions that occur when running the program:
enter a word, phrase, or sentence (blank line to stop):
<enter>
Thank you for using PalindromeTester.
enter a word, phrase, or sentence (blank line to stop):
Ada
palindrome
enter a word, phrase, or sentence (blank line to stop):
This homework is easy
Not a palindrome
enter a word, phrase, or sentence (blank line to stop):
W , ' a " Y . y;a w
palindrome
--------------------------
IMPORTANT:
Please, so far we have only seen one "method", the main method only. In the code please, even though it is corrent, do not create another method like "public static boolean isPalindrome(String input )" is there a way you can make the program work within the "public static void main(String args[])"? We have only covered These topics: Standard Input and Output, Decision Structures, Formatted Output(this includes if statements, nested if statements etc) and Loops and Files.
CODE HERE
import java.util.*;
public class palindrome
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
String input;
System.out.println("enter a word, phrase, or sentence (blank line to stop):");
input=in.nextLine();
while(input.length()!=0)
{
if(isPalindrome(input))
System.out.println("palindrome");
else
System.out.println("NOT a palindrome");
System.out.println("enter a word, phrase, or sentence (blank line to stop):");
input=in.nextLine();
}
System.out.println("Thank you for using PalindromeTester.");
}
public static boolean isPalindrome(String input )
{int i=0,j;
boolean y=false;
j=input.length()-1;
while(Character.toUpperCase(input.charAt(i))==Character.toUpperCase(input.charAt(j))&&i<=j)
{do
{
i++;
}while(!Character.isLetter(input.charAt(i)));
do
{
j--;
}while(!Character.isLetter(input.charAt(j)));
}
if(i>j)
y=true;
return y;
}
}
Explanation / Answer
please rate - and thanks
import java.util.*;
public class palindrome
{
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
String input;
int i,j;
System.out.println("enter a word, phrase, or sentence (blank line to stop):");
input=in.nextLine();
while(input.length()!=0)
{i=0;
j=input.length()-1;
while(Character.toUpperCase(input.charAt(i))==Character.toUpperCase(input.charAt(j))&&i<=j)
{do
{
i++;
}while(!Character.isLetter(input.charAt(i)));
do
{
j--;
}while(!Character.isLetter(input.charAt(j)));
}
if(i>j)
System.out.println("palindrome");
else
System.out.println("NOT a palindrome");
System.out.println("enter a word, phrase, or sentence (blank line to stop):");
input=in.nextLine();
}
System.out.println("Thank you for using PalindromeTester.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.