New course! Every coder should learn Generative AI!
Try a free lesson+ 3
ans is already String then why this line
String p=Character.toString(ans);
You can't compare two String with == assignment. To compare String use equals method like this
a.equals(b)
0
now:
ans=""; // added, always reset ans for new token test
for(int j=l-1; j>=0; j--)
{
// ans = ans+Character.toString( s.charAt(j) );
ans += a.charAt(j); // check individual token a, instead whole input string s
}