In this article, we will write a Palindrome program in Java to check if a String is a Palindrome.
What is Palindrome ?
Palindrome is a word, phrase, or sequence that reads the same backward as forward.
For Example – madam, radar etc
Different Ways to write Palindrome Program in Java for String Input
We will use two ways to find out if the input is a Palindrome or not.
1. Looping through the String till the halfway mark moving one at a time both ways. I mean forward and backwards. Then check the equality of the forward and backward characters. Return false if they are not same.
At max, we will loop through just half of the length of the input String. So, if the length of the string is 50, we will loop maximum of 25 times.
Let’s say the input is “abcdef”, then we will get out of it in the first loop itself as the first “a” and last “f” character are different.
2. Reverse the input String and check if the input String and reversed String are equal or not. Here in the below example , I am using reverse API of StringBuffer for getting the reversed String. Please visit Reverse a String without Using inbuilt StringBuffer reverse API or any Other Utilities for reversing the String in other ways as well.
public class Palindrome {
// Loop through the String till the halfway mark moving one at a time and
// then compare if forward and backward characters are same or not
// If not then it is not a Palindrome
public static boolean isPalindrome(String input) {
for (int i = 0; i < input.length() / 2; i++) {
Character forwardCharacter = input.charAt(i);
Character backwardCharacter = input.charAt(input.length() - 1 - i);
if (!forwardCharacter.toString().equalsIgnoreCase(backwardCharacter.toString())) {
return false;
}
}
return true;
}
//Using StringBuffer's reverse API
public static boolean isPalindromeUsingReverseAPI(String input) {
return input.equalsIgnoreCase(new StringBuffer(input).reverse().toString());
}
public static void main(String[] args) {
System.out.println(isPalindrome("Test String"));
System.out.println(isPalindrome("1221"));
System.out.println(isPalindrome("madam"));
System.out.println(isPalindrome("Madam"));
System.out.println(isPalindromeUsingReverseAPI("dad"));
System.out.println(isPalindromeUsingReverseAPI("test"));
}
}
false
true
true
true
true
false
Different ways to write Palindrome Program in Java for Integers
Here, we will check if the integer is Palindrome or not. For example 121,1221 are Palindrome Integers.
Using String Conversion: Palindrome Program in Java for Integers
import java.util.Scanner;
public class PalindromeInteger {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
scanner.close();
String numStr = Integer.toString(num);
String reversedNumStr = new StringBuilder(numStr).reverse().toString();
if (numStr.equals(reversedNumStr)) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
}
Output
Enter an integer: 121
Palindrome
Using Arithmetic Operations: Palindrome Program in Java for Integers
import java.util.Scanner;
public class PalindromeInteger {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
scanner.close();
int originalNum = num;
int reversedNum = 0;
while (num > 0) {
int remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
}
Output
Enter an integer: 12321
Palindrome
Using Recursion: Palindrome Program in Java for Integers
import java.util.Scanner;
public class PalindromeInteger {
static boolean isPalindrome(int num, int originalNum, int reversedNum) {
if (num == 0) {
return originalNum == reversedNum;
}
int remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
return isPalindrome(num / 10, originalNum, reversedNum);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = scanner.nextInt();
scanner.close();
if (isPalindrome(num, num, 0)) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
}
Output
Enter an integer: 45654
Palindrome
Conclusion: Palindrome Program in Java
A word, phrase, number, or collection of characters that reads the same forwards as it does backwards is known as a palindrome. In other words, even if the order is reversed, it doesn’t alter. Words like “radar” and sentences like “madam, in Eden, I’m Adam” as well as numbers like “12321” are examples of palindromes, which are frequently utilized as a playful linguistic or mathematical idea. Palindromes are curious and occasionally even amusing linguistic constructions because of their symmetry and reversal.
We’ve demonstrated Java’s power in solving different problems by exploring both string and integer palindrome programs. Each method has its own advantages and teaches us about solving problems with algorithms. We’ve used techniques like comparing strings directly, reversing numbers with math, and creating elegant solutions with recursion.
Also Read : Success Strategies: Top 28 Java Programs asked in Interviews