Thursday, November 15, 2012

Palindrome

Palindrome

below is my take on the palindrome challenge posted by a friend on FB
Code #2: Make a function 'is_palindrome' where it takes string as an input and returns a Boolean value if it is a palindrome or not. The function should follow recursive function calls so for/while loops should not be seen in the code. 
public class Palindrome {
public static void main(String [] args)
{
Palindrome p = new Palindrome();
boolean isPalindrome = p.isPalindrome(args[0]);
System.out.println( isPalindrome);
}
public boolean isPalindrome (String word) {
if (word.length() == 1)
return true;
if (word.charAt(0) == word.charAt(word.length() - 1) ){
return isPalindrome(word.substring(1, word.length() - 1 ) );
}
return false;
}
}
view raw Palindrome.java hosted with ❤ by GitHub

No comments: