Tuesday, April 2, 2013

Java: String Pailndrome

/*
Definition: A palindrome is a string that reads the same forward and backward.
For example, "abcba" is a palindrome, "abab" is not.
Please implement this method to
return true if the parameter is a palindrome and false otherwise.
*/

 

  1. public class Palindrome {

  2. /**

  3. * @param args

  4. */

  5. public static void main(String[] args) {

  6. // TODO Auto-generated method stub

  7. System.out.println(isPalindrome("cbc"));

  8. }

  9. public static boolean isPalindrome(String s) {

  10. /*

  11. Definition: A palindrome is a string that reads the same forward and backward.

  12. For example, "abcba" is a palindrome, "abab" is not.

  13. Please implement this method to

  14. return true if the parameter is a palindrome and false otherwise.

  15. */


  16. String temp;

  17. int len = s.length();

  18. char []arr = new char[len];

  19. for(int i=len-1,j=0;i>=0;i--,j++)

  20. {


  21. arr[j]=s.charAt(i);

  22. }


  23. temp = new String(arr);

  24. System.out.println("temp "+temp+"\ns "+s);

  25. if(temp.equals(s))

  26. return true;

  27. return false;

  28. }

  29. }


Download Link: Click Here

0 comments: