Problem type: Recursion / Java Data Structures Problem: Extend the following recurive method so that, in testing to see whether s is a palindrome, nonletters are ignored and no distinctin is made between uppercase and lowercase letters. hint: The toUpperCase() method in the String class will convert a String to uppercase.
Recursive Method: //postcondition: true has been returned if the substring of s at indexes i through j is a palindrome. Otherwise, false has been returned public boolean isPalindrome (String s, int i, int j){ if (i >= j) return true; if (s.charAt (i) != s.charAt (j)) return false; return isPalindrome (s, i+1, j-1); } //method isPalindrome
------------------------------------------------------ A posted complete solution would be awesome! and thank you in advance!
I don't see why this problem requires recursion. Anyway, here is a solution, your only task is to translate it to java, which should be trivial.
import string,sys
def isPalindrome(s):
s = ''.join(s.split()).lower()
for punct in string.punctuation:
s = ''.join(s.split(punct))
for i in range(len(s)/2):
if s[ i ] != s[-1-i]:
return 0
return 1
if __name__ == '__main__':
if len(sys.argv) == 1:
print "Gimme a sentence on the command-line and I'll tell you if it's a palindrome!"
else:
s = ' '.join(sys.argv[1:])
negation = ("n't",'')
print '"%s" is%s a palindrome!' % (s,negation[isPalindrome(s)])