java program Please save the program with the name Palindrom
java program:
Please save the program with the name ‘Palindrome.java’
Write a method which return true if the string parameter is a palindrome.
This is the output example:
Lets check if a string is a palindrom
Please give me a string: abc
No, its not a Palindrom
Lets check if a string is a palindrom
Please give me a string: noon
Yes, its a Palindrom
Solution
//Program for checking string is palindrome or not.
//method isPalindrome is given which returns true when string is palindrome otherwise false
import java.util.Scanner;
public class Palindrome
{ //method for palindrome checking
public static boolean isPalindrome(String str)
{
if(str.length() == 0 || str.length() == 1)
// if length =0 OR 1 then it is
return true;
if(str.charAt(0) == str.charAt(str.length()-1))
// initially check for first and last char of String:
// if they are same then do the same thing for a substring.
return isPalindrome(str.substring(1, str.length()-1));
// if its not the case than string is not.
return false;
}
public static void main(String[]args)
{
Scanner s = new Scanner(System.in);
System.out.println(\"Lets check if a string is a palindrom\");
System.out.print(\"Please give me a string:\");
String str = s.nextLine();
if(isPalindrome(str))
System.out.println(\"Yes, its a Palindrom\");
else
System.out.println(\"No, its not a Palindrom\");
}
}
/* Output
Lets check if a string is a palindrom Please give me a string:abc No, its not a Palindrom
Lets check if a string is a palindrom Please give me a string:noon Yes, its a Palindrom
*/

