Looking to explore Palindrome in Java? Let’s discuss the details in this post.
Introduction to Palindrome In Java?
A palindrome is a phrase, number, digit, word, or sequence of characters which can read the same from backward as forward.
Let’s see some of the examples of the Palindrome algorithm.
Numeric Palindrome
Palindrome Numbers:
11, 121, 333, 5555
Palindrome Time:
12:21, 13:31, 11:11
Palindrome Words, Sentences, Names
1. Palindrome Words
rotor, civic, level, racecar
2. Palindrome Names
anna, eve, bob
3. Palindrome Sentences
was it a car or a cat i saw, mr owl ate my metal worm, do geese see god.
Using Java Implementation
Let’s write a Java Palindrome program to check whether the given number is Palindrome or not.
Here the input we provided in the below Java program is number 151(declared as integer variable).
In this case, we are using for loop logic for iteration.
We declared temp variable “actualValue”, to store the original string and compare it with reversed string.
Palindrome Number Validation
public static void main(String[] args) {
try {
int number = 151;
int reverseValue = 0;
int reminder;
int actualValue;
actualValue = number;
for(;number!= 0; number /= 10)
{
reminder = number % 10;
reverseValue = reverseValue * 10 + reminder;
}
if (actualValue == reverseValue)
System.out.println(actualValue + " is a valid palindrome.");
else
System.out.println(actualValue + " is not a valid palindrome.");
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
151 is a valid palindrome.
As per the above output step, we can see the original input number 151 is matching with reversed number 151 which results in invalid palindrome.
Let’s check the same implementation by using a while loop iteration in below Java program.
public static void main(String[] args) {
try {
int number = 151;
int reverseValue = 0;
int reminder;
int actualValue;
actualValue = number;
while (number != 0) {
reminder = number % 10;
reverseValue = reverseValue * 10 + reminder;
number /= 10;
}
if (actualValue == reverseValue)
System.out.println(actualValue + " is a valid palindrome.");
else
System.out.println(actualValue + " is not a valid palindrome.");
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
151 is a valid palindrome.
As per the above output, the original number is matching with the reversed number.
In other words, it should be the same from first to the last digit and last to the first digit.
You May Like,
- How to remove a character from a String in Python
- 4 Ways to Remove Character from String in JavaScript.
Palindrome String Validation
in this section, you will know palindrome string validation in palindrome in java.
Let’s write a Palindrome program to check whether the input string is Palindrome or not.
Here is an example.
public static void main(String[] args) {
try {
String inputValue = "rotor";
String reverseValue = "";
int length = inputValue.length();
for ( int x = length - 1; x >= 0; x-- )
reverseValue = reverseValue + inputValue.charAt(x);
if (inputValue.equals(reverseValue))
System.out.println(inputValue + " is a valid palindrome.");
else
System.out.println(inputValue + " is not a valid palindrome.");
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
rotor is a valid palindrome.
As per the above output, the input character is matching with the reversed character.
Palindrome Validation using Reverse Method
in this section lets validate palindrome in java using the reverse method.
Let’s leverage the Java library method reverse() of StringBuffer.
Using the Palindrome program below, let’s use the reverse method to check whether the given string is Palindrome or not.
The program will compare the original string with the reversed string and delivers the result accordingly.
public static void main(String[] args) {
try {
String inputValue ="Ana";
String reverseValue = new StringBuffer(inputValue).reverse().toString();
if (inputValue.equalsIgnoreCase(reverseValue))
System.out.println(inputValue + " is a valid palindrome.");
else
System.out.println(inputValue + " is not a valid palindrome.");
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
Ana is a valid palindrome.
As per the output above, we can see the input is matching with reverse order or reverse string and thus results in invalid Palindrome.
To conclude this tutorial, we covered various types of solution to validate Palindrome in Java.