Hacking Exposed : Network Security Secrets and Solutions No. 6 by Stuart…
Current Bid: $18.00
Java Program: How to make a Palindrome Test in Java? Sample Java source code is given.
Recently, we just finished the reverse string java program. We will use this program as a basis to have a Palindrome test program. If you do not know about Palindrome, it is a word or a group of word that reads and looks exactly the same as the original text when reversed. For example, the famous Palindrome word is ma’am, so whether you read it from first letter to last letter or from last letter to first letter, the word is still the same. Therefore, the word ma’am is a palindrome. So, in this program we will test each string of words entered by the user if it is a Palindrome or Not and print the result on the screen.
Java Source Code for Palindrome Test
// main class
package ispalindrome;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner Myscan = new Scanner(System.in);
String word = “”;
System.out.print(“Enter a String: “);
word = Myscan.nextLine();
is_Palindrome access = new is_Palindrome();
if(access.isPalindrome(word) == true)
System.out.println(word + ” is a PALINDROME.”);
else
System.out.println(word + ” is NOT A PALINDROME.”);
}
}
//java class
package ispalindrome;
public class is_Palindrome
{
public boolean isPalindrome(String word_entered)
{
if(word_entered.length() == 1 )
return true;
else if (word_entered.length()==2)
{
if(word_entered.charAt(0) == word_entered.charAt(word_entered.length()-1))
{
return true;
}
}
else
{
if(word_entered.charAt(0) == word_entered.charAt(word_entered.length() – 1))
{
return isPalindrome(word_entered.substring(1, word_entered.length() – 1));
}
else
return false;
}
return false;
}
}
Sample Output:
Related Java Tutorials:
Complete List of Java Tutorial and Source Codes for Absolute Beginners
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming