A string can be said to be a palindrome string if the reverse of that string gives the same result as the original. The palindrome program in java checks for the strings and numbers to see if the output is the same as the original input.
For string, we can consider an example for the words like LoL, radar, level, etc. In these words, the string is the same as the original string. In easy words, we say that any number that is equal to the reverse of exactly its number is called a palindrome number.
For the palindrome number program in Java, the string is checked like 3553, 12321, etc. It’s a simple process of reversing a number first and then checking by matching those numbers with the initial value as a comparison.
Palindrome Program in Java Using Reverse Method
We see a small example of a string palindrome program in Java using the reverse method below.
System.out.println("Enter any string for palindrome"); original = in. nextLine(); for (int i = length - 1; i >= 0; i--) reverse = reverse + original. charAt(i); if (original. equals(reverse)) System.out.println("The string is a palindrome."); else System.Out.
A string can be said to be a palindrome only and only if the string that is input and read from left to right is identical and same in the reverse form i-e; from right to left. The string is based upon ignoring any upper and lower case differences of letters.
Considering an example here will be, the string “RaceCar” is a palindrome, while any string like “iGattiNonAvevanoCugini” is not a string. The Palindrome program in java for string just makes a comparison to see the identical output as the input. You can use the palindrome program in Java for a string by using a loop too.
Also Read: Armstrong Number In Java
Palindrome Program in Java Using for Loop
import java.util.Scanner; class PalindromeTest { public static void main(String args[]) { String reverseString=""; Scanner scanner = new Scanner(System.in); System.out.println("Enter letters to check if they are palindrome:"); String inputString = scanner.nextLine(); int length = inputString.length(); for ( int i = length - 1 ; i >= 0 ; i-- ) reverseString = reverseString + inputString.charAt(i); if (inputString.equals(reverseString)) System.out.println("The string is a palindrome."); else System.out.println("The string is not a palindrome."); } }
Output 1:
Enter letters to check if they are palindrome:
aabbaa
the string is a palindrome.
Output 2:
Enter letters to check if they are palindrome:
aaabbb
the string is not a palindrome.
Conclusion
This was a description for the palindrome program in Java with a string example as well as other info. For more info, leave a reply below.
FAQ’s
How to write a palindrome in Java?
Palindrome Program in Java
- out.println(“Enter data to check if it’s a palindrome”);
- original = in.nextLine();
- for (int i = length – 1; i >= 0; i–)
- reverse = reverse + original.
- charAt(i);
- if (original. equals(reverse))
- out. println(“This is a palindrome.”);
- out.