Problem: Write a java program to check whether the given string is palindrome or not.
There are multiple ways to check for a palindrome string in Java. Let’s discuss some of the methods with examples.
Check Palindrome String in Java using for Loop
In this method, we reverse the string using a loop and compare the reverse with the original input string.
To reverse the string we need to loop through each character of the original string from the back and concatenate it into the reverse string.
import java.util.Scanner;
public class PalindromeString {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String str, rev="";
System.out.println("Enter a String");
str= in.nextLine();
for(int i=str.length()-1 ; i>=0; i--){
rev = rev + str.charAt(i);
}
if(rev.equalsIgnoreCase(str))
System.out.println("Palindrome String");
else
System.out.println("Not a Palindrome String");
}
}
Output
Enter a String mom Palindrome String Enter a String wow wow Palindrome String Enter a String pencil programmer Not a Palindrome StringCheck Palindrome String in Java Without Finding its Reverse
To check whether a string or word is palindrome or not without computing reverse, we need to compare 1st half of the string with another half.
We can achieve this by using for loop from 0 to half the length of the input string (i.e. str.length()/2
).
import java.util.Scanner;
public class PalindromeStringWR {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int l;
boolean flag = true; //Assuming string is palindrome
String str;
System.out.println("Enter a String");
str= in.nextLine();
l = str.length(); //length of string
for(int i=0; i<=l/2; i++){
//If at a particular position from front if the character doesn't match
//with the character at same position from last, then string is not palindrome
if(str.charAt(i) != str.charAt(l-1-i)){
flag = false;
break;
}
}
if(flag == true)
System.out.println("Palindrome String");
else
System.out.println("Not a Palindrome String");
}
}
Output
Enter a String madam Palindrome String Enter a String hello! Not a Palindrome StringCheck Palindrome String in Java using StringBuilder
The StringBuilder class in Java has an inbuilt reverse()
method, that can be used to reverse a string.
All we need to do is to compare the input string with the return result of the reverse()
method.
Here is the Java code for the same.
import java.util.Scanner;
public class PalindromeStringSB {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String str, rev="";
System.out.println("Enter a String");
str= in.nextLine();
StringBuilder stringBuilder = new StringBuilder(str);
rev = stringBuilder.reverse().toString();
if(str.equalsIgnoreCase(rev))
System.out.println("Palindrome String");
else
System.out.println("Not a Palindrome String");
}
}
Output
Enter a String lol Palindrome String Enter a String please comment Not a Palindrome StringIn this tutorial, we learned different ways to check palindrome string in Java programming language.