## Sample Code
### Count the number of "e"s in a sentence
```java
public class RCount
{
RCount()
{
System.out.println(count("I never saw a purple cow"));
}
int count(String s)
{
if (s.length()==0)
return 0;
else if (s.substring(0,1).equals("e"))
return 1 + count(s.substring(1, s.length()));
else
return count(s.substring(1, s.length()));
}
public static void main (String args [])
{
new RCount();
}
}
```
### Reverse a String
```java
public class RevString
{
RevString()
{
System.out.println(reverseH("I never saw a purple cow"));
}
String reverseH(String s)
{
if (s.length() == 0) return "";
else if (s.length() == 1) return s;
else return reverseH(s.substring(1)) + s.substring(0,1);
}
public static void main (String args [])
{
new RevString();
}
}
```
### Sum a series of Digits
Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
```java
public class Recurs
{
Recurs()
{
System.out.println(sumDigits(12345));
}
public int sumDigits(int val)
{
if (val<10)
{
return val;
}
else
{
return sumDigits(val/10) + val%10;
}
}
public static void main(String[] args)
{
new Recurs();
}
}
```
## Exercise
1. The first 6 triangle numbers are 0, 1, 3, 6, 10, 15. The nth triangle number is 1 + 2 + 3 + … + n. Write a recursive method to find the nth triangle number
2. Write a recursive method that returns m to the nth power, e.g. 2 to the power of 3 returns 8.
3. The Harmonic Series begins 1 + 1/2 + 1/3 + 1/4 + 1/5 + … Write a recursive method that finds the Harmonic Series up to the nth term.
4. Write a recursive method that counts the number of times "hi" appears in a string. Example countHi("hi ho, hi ho. It's high time… " will return 3.
5. The Fibonacci Series begins 1,1,2,3,5,8,13,… The next digit is the sum of the last two digits, so 1 + 1 = 2, 1 + 2 = 3 etc. Write a recursive method to print the nth fibonacci number
6. Write a recursive method that checks if string is a palindrome
7. Write a recursive method called reduce() that returns a String with repeated characters removed. Example: reduce("mmmississippi") returns "misisipi"