CS173: Intro to Computer Science - Recursion (3 Points)

Developed by Professor Tralie and Professor Mongan.

Exercise Goals

The goals of this exercise are:
  1. To implement a recursive function call
Fill in the reverseString method below to recursively compute the string in reverse.
Welcome to our online modules system! Be sure to log in with your Urinus ID before you proceed. For example, Professor Tralie's ID is ctralie. If you are not an Ursinus student, that's fine! Just make something up, and you can still run everything

                    



In this exercise [1], the main() method runs the code on “hello” and “stressed,” and they should give “olleh” and “desserts,” respectively (remember that when you’re feeling stressed during finals week!).

Hints

  • Since s.length()-1 is the last index of a string s, the last character of s can be extracted with s.charAt(s.length()-1)
  • Recall from chapter 2 that for a string s, s.substring(a, b) gives the substring of a string from index a to index b, not including b. So, for instance, if s = “stressed”, then s.substring(0, 7) would yield the string “stresse”. In this case, index 7 is the last index, but we don’t include that index.
  • If you get an error like too much recursion, this is the same as the stack overflow error we saw in the video. This probably means that the string you’re calling recursively isn’t actually decreasing in length, so you never reach the stopping condition of an empty string, and your code goes on forever. Check to make sure you’re taking the right substring in your recursive call.
  1. Developed by Prof. Chris Tralie