In this tutorial, we have to check whether the given string is palindrome or not in Java. Here we will see how to reverse the string and compare it with the other string in Java.
A palindrome is a word, number phrase, a sequence that reads the same backwards and forwards. (To know more about palindrome.).
Here we are using the concept of reversing string and storing it in other string and then comparing both the strings using the "equals()" method.
We are storing the string A in String B character-wise.
import java.util.*; public class StringPalindrome { public static void main(String[] args) { String B=""; Scanner sc=new Scanner(System.in); String A=sc.next(); int length=A.length(); for(int i=length-1;i>=0;i--) { B=B+A.charAt(i); } if(A.equals(B)) { System.out.println("Yes"); } else { System.out.println("No"); } } }
Output:-
Submitted by Kashish Naresh Dhoot (Kashish1104)
Download packets of source code on Coders Packet
Comments