Java - Write code to count subString with recursive function

Requirements

Write code to count subString with recursive function

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str1 = "book2s.com";
        String str2 = "o";
        System.out.println(countStr(str1, str2));
    }// w w  w . ja  v  a 2  s.  co m

    public static int countStr(String str1, String str2) {
        int counter = 0;
        if (str1.indexOf(str2) == -1) {
            return 0;
        } else if (str1.indexOf(str2) != -1) {
            counter++;
            counter += countStr(
                    str1.substring(str1.indexOf(str2) + str2.length()),
                    str2);
            return counter;
        }
        return 0;
    }
}

Related Exercise