13-recursive_programming

This preview shows pages 10–15. Sign up to view the full content.

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

This preview has intentionally blurred sections. Sign up to view the full version.

View 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); } }
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)); } }

This preview has intentionally blurred sections. Sign up to view the full version.

View 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?
14 Reversal pseudocode Reversing the lines of a file: Read a line L from the file.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern