Ispalindromemadam true ispalindromeracecar true

This preview shows 10 out of 15 pages.

– isPalindrome("madam") true – isPalindrome("racecar") true – isPalindrome("step on no pets") true – isPalindrome("able was I ere I saw elba") true – isPalindrome("Java") false – isPalindrome("rotater") false – isPalindrome("byebye") false – isPalindrome("notion") false
Image of page 10

Subscribe to view the full document.

11 Exercise solution // Returns true if the given string reads the same // forwards as backwards. // Trivially true for empty or 1-letter strings. public static boolean isPalindrome(String s) { if (s.length() < 2) { return true; // base case } else { char first = s.charAt(0); char last = s.charAt(s.length() - 1); if (first != last) { return false; } // recursive case String middle = s.substring(1, s.length() - 1); return isPalindrome(middle); } }
Image of page 11
12 Exercise solution 2 // Returns true if the given string reads the same // forwards as backwards. // Trivially true for empty or 1-letter strings. public static boolean isPalindrome(String s) { if (s.length() < 2) { return true; // base case } else { return s.charAt(0) == s.charAt(s.length() - 1) && isPalindrome(s.substring(1, s.length() - 1)); } }
Image of page 12

Subscribe to view the full document.

13 Exercise • Write a recursive method reverseLines that accepts a file Scanner and prints the lines of the file in reverse order. Example input file: Expected console output: Roses are red, Are belong to you. Violets are blue. All my base All my base Violets are blue. Are belong to you. Roses are red, What are the cases to consider? • How can we solve a small part of the problem at a time? • What is a file that is very easy to reverse?
Image of page 13
14 Reversal pseudocode • Reversing the lines of a file: Read a line L from the file.
Image of page 14

Subscribe to view the full document.

Image of page 15
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern