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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
No comments:
Post a Comment