In this post, we will write a Java program to check if the input String is Palindrome or not.
A String is said to be Palindrome if its value is the same when reversed. For example “aba” is a Palindrome String.
In this post, we will see three different ways we can write a program to check if the input String is Palindrome or not.
1. Using concatenation + operator
In this program, we use the concatenation + operator to reverse a string. The reverse string is checked for equality with the input original string, if both the strings are equal then the string is Palindrome./**
* Java Program to Check if Input String is Palindrome
* @author javaguides.net
*
*/
public class StringPalindromeProgram {
private static boolean isEmpty(final String cs) {
return cs == null || cs.length() == 0;
}
public static boolean checkPalindrome(String input) {
// Check error conditions
if (isEmpty(input)) {
return false;
}
String reverse = "";
int length = input.length();
for (int i = length - 1; i >= 0; i--) {
reverse = reverse + input.charAt(i);
}
if (input.equals(reverse)) {
System.out.println(input + " is palindrome = " + true);
} else {
System.out.println(input + " is palindrome = " + false);
}
return false;
}
public static void main(String[] args) {
checkPalindrome("madam");
checkPalindrome("abcba");
checkPalindrome("abc");
}
}
Output:
madam is palindrome = true
abcba is palindrome = true
abc is palindrome = false
2. Palindrome Program with Simple Logic
Let's compare characters in the String from both ends to find out if it’s a palindrome or not./**
* Java Program to Check if Input String is Palindrome
* @author javaguides.net
*
*/
public class StringPalindromeProgram {
private static void checkPalindromeString(String input) {
boolean result = true;
int length = input.length();
for (int i = 0; i < length / 2; i++) {
if (input.charAt(i) != input.charAt(length - i - 1)) {
result = false;
break;
}
}
System.out.println(input + " is palindrome = " + result);
}
public static void main(String[] args) {
checkPalindromeString("madam");
checkPalindromeString("abcba");
checkPalindromeString("abc");
}
}
Output:
madam is palindrome = true
abcba is palindrome = true
abc is palindrome = false
3. Using StringBuilder reverse() Method
The String class doesn’t provide any method to reverse the String but StringBuffer and StringBuilder class has a reverse() method that we can use to check if the String is palindrome or not.public class StringPalindromeProgram {
private static boolean isPalindrome(String str) {
if (str == null)
return false;
StringBuilder strBuilder = new StringBuilder(str);
strBuilder.reverse();
return strBuilder.toString().equals(str);
}
public static void main(String[] args) {
System.out.println(isPalindrome("madam"));
System.out.println(isPalindrome("abcba"));
System.out.println(isPalindrome("abc"));
}
}
Output:
true
true
false
Related Java String Programs with Output
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Swap Two Strings Without Using Third Variable
Comments
Post a Comment
Leave Comment