public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String A=sc.next();
StringBuffer a1 = new StringBuffer();
a1.append(A);
System.out.println(a1.equals(a1.reverse())?"Yes":"No");
}
Asked
Active
Viewed 39 times
-1

Pramod
- 1,376
- 12
- 21

Rohit Behera
- 3
- 2
-
Does this answer to your question https://stackoverflow.com/questions/4138827/check-string-for-palindrome – Prathamesh More Sep 13 '20 at 16:19
-
Does this answer your question? [Check string for palindrome](https://stackoverflow.com/questions/4138827/check-string-for-palindrome) – Abra Sep 13 '20 at 16:58
2 Answers
1
This will always return yes
as a1.reverse()
returns original StringBuffer
object which is same as a1
. If you want to compare string, you have to generate string from stringbuffer object:
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
String A=sc.next();
StringBuffer a1 = new StringBuffer();
a1.append(A);
System.out.println(a1.toString().equals(a1.reverse().toString())?"Yes":"No");
}

Pramod
- 1,376
- 12
- 21
0
Yes by converting it into a string first you would be able to compare them . Otherwise you would be comparing there address(StringBuffer address),which is same is case of both.