java - Tell me someone how to print string is palindrome or not -


i trying print string palindrome or not

  import java.util.*;   import java.lang.*;    class testclass {        public static void main(string args[] ) throws exception {        scanner s=new scanner(system.in);        int n=s.nextint();        string s1="";        string rev="";        for(int i=0;i<n;i++){         s1=s.next();         stringbuilder sb=new stringbuilder(s1);         stringbuilder re=new stringbuilder(rev);         re=sb.reverse(); 

when assign sb value re apply condition check if after reversal string equal or not

          if(re.tostring().equals(sb.tostring())){                 system.out.println("yes");               }             else{                system.out.println("no");            }         }       } } 

but it's not working it's printed yes whether palindrome or not

the problem called reverse() on stringbuilder, not on string. reverse character sequence of stringbuilder. , in code, both variables point same stringbuilder, output "yes". change way create second stringbuilder:

stringbuilder sb = new stringbuilder(s1); //create new stringbuilder s1 sequence, reverse stringbuilder re = new stringbuilder(s1).reverse(); 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -