class palindrome
{
public static void main(String args[])
{
String s1=new String();
Scanner sc= new Scanner(System.in);
System.out.println("Enter the string:");
s1=sc.nextLine();
StringBuffer s2=new StringBuffer(s1);
s2.reverse().toString();
if(s1.equals(s2.toString()))
System.out.println("Given String is palindrome");
else
System.out.println("Given String is not palindrome");
}
}
This is my code to check whether a string
is palindrome or not.
I get the correct output, but I have 2 questions:
1) Why cannot we use toString
like s1.toString()
2) If I write if(s1.equals(s2))
instead of just using an if
condition is skipped and directly else
condition is ran in output, why so?