Palindrome Detection ? A palindrome is a word or phrase that can be read the sam
ID: 3532980 • Letter: P
Question
Palindrome Detection
? A palindrome is a word or phrase that can be read the same way in either direction (the
adjustment of punctuation and spaces between words is generally permitted). Examples
of palindromes are: (1) civic, (2) Rise to vote sir.
? Write a Java program that reads in a user-entered string and determine whether it is a palindrome
or not. Consider using the trim() function in the String class to get rid of all the empty spaces.
? Your program must use a recursive Java function to determine whether or not a given string is a
Palindrome.
? A sample input of your program could be the following:
Please enter the String: Rise to vote sir
Rise to vote sir is a Palindrome.
Explanation / Answer
import java.io.*;
class Palin
{
public static boolean pal(String s)
{
String t="";
for(int i=s.length()-1;i>=0;i--)
{
t+=s.charAt(i);
}
if(t.equals(s))
return true;
else
return false;
}
public statci void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string:");
String m="",n=br.readLine();
n=n.trim();
for(int i=0;i<n.length();i++)
{
if(n.charAt(i)!=' ')
m+=n.charAt(i);
}
if(pal(m))
System.out.println(n+" is palindrome.");
else
System.out.println(n+" is not a palindrome.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.