Tuesday, May 8, 2012

String Pallendrom Checking in Java


public class Pallendrom {

      public boolean isPallendrom(String str) {
            char[] chars = str.toCharArray();
            int lchar = chars.length;
            for(int i=0; i<lchar/2; i++){
                  if(chars[i]!=chars[lchar-1-i]){
                        return false;
                  }
            }
            return true;
      }

      public static void main(String[] args) {
            String a = "12321";
            Pallendrom p = new Pallendrom();
            if(p.isPallendrom(a)){
                  System.out.println(a + " is Pallendrom.");
            }else{
                  System.out.println(a + " is not Pallendrom.");
            }
      }
}


No comments:

Post a Comment