The Artima Developer Community
Sponsored Link

Java Answers Forum
the third of its kind...

1 reply on 1 page. Most recent reply: Mar 10, 2002 8:00 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Amber K

Posts: 6
Nickname: amberk
Registered: Mar, 2002

the third of its kind... Posted: Mar 10, 2002 9:42 AM
Reply to this message Reply
Advertisement
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!

Amber :)


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: the third of its kind... Posted: Mar 10, 2002 8:00 PM
Reply to this message Reply
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)])

Flat View: This topic has 1 reply on 1 page
Topic: Run Time Error in my program. Previous Topic   Next Topic Topic: I keep getting an error message, I cant quite see whats wrong in my code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use