To Check whether String is Palindrome or not using Java Program, First Let’s know about Palindrome.
What is Palindrome?
A String is called a palindrome, when it is not changed either we write it from starting point or the last end of the String.
For Example: “kayak”
If we write it from the start or the end, we will get “kayak”.
String Palindrome Program in Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("String Palindrome program in Java ");
System.out.println("Please give String : ");
String str= sc.nextLine();
StringBuilder strRev = new StringBuilder();
strRev.append(str);
strRev = strRev.reverse();
if(str.equals(strRev.toString())){
System.out.print("String is palindrom");
}else{
System.out.print("String is not palindrom");
}
}
}
Output 1:
String Palindrome program in Java
Please give String :
kayak
String is palindrom
Output 2:
String Palindrome program in Java
Please give String :
tutor
String is not palindrom