The software I use is Eclipse, please teach me how to write it on Eclipse. Write
ID: 3727653 • Letter: T
Question
The software I use is Eclipse, please teach me how to write it on Eclipse.
Write a method that checks whether the input string or a sentence (a string with spaces) is a palindrome or not. The method should be case insensitive and should ignore spaces. Write a test program that prompts the user to input a string and invokes this method. Some example runs are: Enter the input string: madam Input string madam is a palindrome Enter the input string: banana Input string banana is NOT a palindrome Enter the input string: Race Car Input string Race Car is a palindrome Enter the input string: TooHOT to hoot Input string Too HOT to hoot is a palindromeExplanation / Answer
// A program to check whether a String is palindrome or not
import java.util.Scanner; //Importing the Sanner library
class Palindrome //Class name
{
// Recursive function that check whether string is palindrome
static boolean isPalinUtil(String str, int start, int end) //Utility function for palindrome
{
// palindrome if only one character
if (start == end)
return true;
// If first and last characters don't match
if ((str.charAt(start)) != (str.charAt(end)))
return false;
// If there are more than two characters,
// check if middle substring is also
// palindrome or not.
if (start < end+1)
return isPalinUtil(str, start+1, end-1);
return true;
}
static boolean isPalindrome(String str) //Function that will call the utility function
{
int len = str.length();
if (len == 0)
return true;
return isPalinUtil(str, 0, len-1);
}
// Main Function
public static void main(String args[])
{
Scanner sc=new Scanner(System.in); //USed for scanning or reading user input
System.out.println("Enter the string ");
String str=sc.nextLine(); //Reading the Input from user
String copy=str;
str = str.replaceAll("[^A-Za-z]", "").toLowerCase().toString(); //ignoring the spaces and making the string case in-sensitive
// System.out.println(str);
if (isPalindrome(str)) //Calling the function which is returning true or false
{
System.out.println("Input String "+copy+" is a palindrome");//if true
} else
System.out.println("Input String "+copy+" is not a palindrome"); //else false
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.