COMP 1672 Sections 1 & 2 Homework #4 Solutions 1. Do problem 2 on page 496 a. The base case is if u is equal to 0 or 1 One of two output statements executes b. The general case is if u is neither 0 nor 1 u is decremented and the function is called recursively c. The letter 'B' is output 2. a. Func is called exactly once, returning in the first base case. The number 2 is output. b. Func is called exactly once, returning in the second base case. The number 3 is output. c. Func is called with the value of 2, the final return is executed, which returns Func(1) + Func (0). By parts a and b, this is 2+3 or 5 d. Func(5) calls: Func(4) + Func(3) Func(4) calls: Func(3) calls: Func(3) + Func(2) Func(2) + Func(1) Func(3) calls:i returns 5 returns 5 returns 3 Func(2)+Func(1) by part c by part c by part b =5+3=8 (by c and b) Func(4) gets back Func(3) gets back 8 + 5=13 5 + 3 = 8 and returns it and returns it Func(5) gets back 13 + 8 = 21, which gets displayed 3. Here is just one solution. There are many! #include #include using namespace std; bool is_palindrome(string s) { if (s.length()==0 || s.length() == 1) return true; if (s[0] != s[s.length()-1]) return false; return is_palindrome(s.substr(1,s.length()-2)); } int main() { string str; cout << "Input the string you want to check to see if it's a palindrome: "; cin >> str; cout << str << (is_palindrome(str)?" is":" isn't") << " a palindrome.\n"; return 0; }