Recursion example 3 reversing a string stop when there

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview:   And the answers start to be returned ... ∑2 gauss(2) call • The stack grows un5l a call that executes a base case is found ∑N-2 ∑1 frame n returns . . . • The difference is that there will be several frames execu5ng the same func5on call   In fact, it was invented precisely to handle recursion main gauss(N) call • The stack handles recursion beau5fully! gauss(1) 26 Recursion and the stack • Try this out on your own!!!! 27 9 13 ­11 ­04 Recursion Example # 3 – Reversing a String •  Want to reverse the characters of a string •  Our basic opera5on is swap the two characters at the beginning and end of the string O R A N G E S S R A N G E O Recursion Example # 3 – Reversing a String •  Now we just need to do that with inner string, and a`ach two outer characters S R E A A N N G G E O R •  … so when do we stop? Recursion Example # 3 – Reversing a String •  Stop when there is nothing to swap •  Either 0 or 1 characters in the string, depending on if the length of the original string was even or odd N N •  So the length == 0 or length == 1 is our base case! 10 13 ­11 ­04 Recursion Example # 3 – Reversing a String r...
View Full Document

This document was uploaded on 03/02/2014.

Ask a homework question - tutors are online