summaryrefslogtreecommitdiff
path: root/in_class/cse123-inclass9/ReverseString.java
blob: f2a5858dfcd1edb5402e28878503cfec50f60a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ReverseString {
    public static void main(String[] args){
        System.out.println(reverse("Hello"));
        System.out.println(reverse("nathan"));
        System.out.println(reverse("racecar"));
        System.out.println(reverse("taco"));
        System.out.println(reverse("ABBA"));
        System.out.println(reverse(""));
        System.out.println(reverse("a"));
    }

    public static String reverse(String str){
        if (str.isEmpty()) {
            return "";
        } else if (str.length() == 1) {
            return str;
        }

        String backwards = reverse(str.substring(1)) + str.charAt(0);
        return backwards;
    }
}