In: Computer Science
This problem is about the java programming and contain two parts
First part:
a word that reads the same forward and backward is called a
palindrome, e.g., "mom", "dad", "racecar", "madam", and "Radar"
(case-insensitive). Write a program called TestPalindrome, that
asks the user for a word and prints whether the word is a
palindrome or not.
Hint: for a string called inStr, you can use inStr. toLowerCase()
which returns a new string which is all in lower case letters. Use
two indexes forwardIndex and backwardIndex to scan the string
forward and backward at the same time.
Give the code of the first part.
The second part is :
copy-paste the TestPalindrome program,Then modify the program to
check a whole sentence (not just one word) to see whether it is a
palindrome or not. Punctuation, spaces, and capitalization must be
ignored.
Here are examples of palindromic sentences:
•Madam, I'm Adam
•A man, a plan, a canal - Panama!
Hint: to read a whole sentence, create a Scanner object called
input and then use input.nextLine(). For a character variable
called c, you can use Character.isLetter(c) to compute a boolean
result indicating whether the character is a letter or not.
The
Solution for the above program is provided below. If any doubt please comment below.
First Part:
TestPalindrome.java
import java.util.Scanner;
public class TestPalindrome {
public static void main(String[] args) {
// New scanner object
Scanner input=new Scanner(System.in);
String inStr;
// Input the word
System.out.print("Enter the word to test it is palindrome:
");
inStr=input.next();
inStr=inStr.toLowerCase();
int backwardIndex=inStr.length()-1;
boolean palindrome=true;
// Check it is palindrome or not
for(int
forwardIndex=0;forwardIndex<inStr.length();forwardIndex++)
{
// If not palindrome
if(inStr.charAt(forwardIndex)!=inStr.charAt(backwardIndex))
{
palindrome=false;
break;
}
backwardIndex--;
}
// Print result
if(palindrome)
{
System.out.println("The given word is palindrome");
}
else
{
System.out.println("The given word is not palindrome");
}
}
}
Output:
Second Part:
TestPalindrome.java
import java.util.Scanner;
public class TestPalindrome {
public static void main(String[] args) {
// New scanner object
Scanner input=new Scanner(System.in);
String inStr;
// Input the line
System.out.print("Enter the line to test it is palindrome:
");
inStr=input.nextLine();
inStr=inStr.toLowerCase();
int backwardIndex=inStr.length()-1;
boolean palindrome=true;
// Check it is palindrome or not
for(int
forwardIndex=0;forwardIndex<inStr.length();forwardIndex++)
{
// If entire line parsed
if(backwardIndex<0)
break;
// Read each character
char c1=inStr.charAt(forwardIndex);
char c2=inStr.charAt(backwardIndex);
// If both are letters
if (Character.isLetter(c1)&&Character.isLetter(c2))
{
// If not palindrome
if(c1!=c2)
{
palindrome=false;
break;
}
backwardIndex--;
}
// If c2 is not letter
if(!Character.isLetter(c2))
{
// Decrement indexes
backwardIndex--;
forwardIndex--;
}
}
// Print result
if(palindrome)
{
System.out.println("The given line is palindrome");
}
else
{
System.out.println("The given line is not palindrome");
}
}
}
Output: