In WordswithPals.java, write a program that inputs a String via the Scanner clas
ID: 3602524 • Letter: I
Question
In WordswithPals.java, write a program that inputs a String via the Scanner class and ther prints whether or not the input String is a palindrome. In your check for palindromes, do not consider capitalization, spaces or punctuation marks (.,-!'"? ) when checking whether a string is a palindrome. Do not use a loop of any kind; you will lose many points if you do. You can use regular expressions if desired, but you will get no extra credit. Dropbox WordswithPals.java. Sample program output: Enter a string: Not a palindrome. Enter a string: That's a palindrome! Enter a string: That's a palindrome! rewarder A man, a plan, a canal, Panama! Was it a car or a cat I saw?Explanation / Answer
As told the program is done without using any loop
import java.util.Scanner;
public class HelloWorld
{
public static void main (String[] args)
{
String str=" "; //variable declaration
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String :"); //aceepting the string from user
str=sc.nextLine();
String str2 = new StringBuffer(str).reverse().toString(); //reversing the //string
if (str.equals(str2)) //checking for palindrome
System.out.println("That's a palindrome");
else
System.out.println("Not a palindrome");
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.