Problem #3 Find Last Occurrence of Character in String
Base Case: Well if you are at the end of the string and no characters follow,
it must be the last occurrence if there is a match.
Recursive Decomposition: Start at the end of the string and after each call remove the last character.
```cpp
size_t last_occurrence(std::string str, char ch)
{
size_t length = str.size();
if(length < 1)
return -1;
else if (str[length -1] == ch)
return length;
return last_occurrence(str.substr(0, length - 1), ch);
}
```
Problem #2 Count number of spaces
Base case: When the string is empty it cannot have a space, so the count of spaces is 0.
Recursive Decomposition: Check character by character by working with substrings of the string,
and then sum the counts of the strings.
```cpp
size_t count_space(std::string str)
{
if(str.size() < 1)
return 0;
else
return ( (str[0] == ' ' )? 1 : 0 ) + count_space(str.substr(1));
}
```
It runs as:
```
count_space("It is")
( 0 + count_space("t is"))
( 0 + ( 0 + count_space(" is") ) )
( 0 + ( 0 + ( 1 + count_space("is") ) ) )
( 0 + ( 0 + ( 1 + ( 0 + count_space("s") ) ) ) )
( 0 + ( 0 + ( 1 + ( 0 + ( 0 + count_space("") ) ) ) ) )
( 0 + ( 0 + ( 1 + ( 0 + ( 0 + 0) ) ) ) )
1
```
Source: [here](http://kurrytran.blogspot.com/2013/07/recursive-interview-questions-tutorial.html)
To solve a problem recursively:
1. Think about how to solve the problem for simple cases (base case).
2. Determine how to break down larger cases into smaller instances (recursive decomposition).
Problem #1 Reverse A String
Base Case: When the string is the empty string is it the same backwards as it is forwards
Recursive Decomposition: For strings, to shrink the string to make forward progress is the same as solving the
problem for substrings of the string, and then combining the sub-solutions.
```cpp
/* Returns the reverse of the indicated string. */
std::string reverse(std::string str) {
if (str.size() < 1)
return "";
else
return reverse(str.substr(1)) + line[0];
}
```
It runs as:
```
reverse("Hello")
(reverse("ello")) + "H"
((reverse("llo")) + "e") + "H"
(((reverse("lo")) + "l") + "e") + "H"
((((reverse("o")) + "l") + "l") + "e") + "H"
(((("o") + "l") + "l") + "e") + "H"
"olleH"
```