scbushan05
7/6/2017 - 10:57 AM

NumberOfOccurences.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaprograms;

/**
 *
 * @author bhushan_chandranna
 */
public class NumberOfOccurences {
    public static void main(String[] args) {
        System.out.println(numberOfOccurences("adfbushansdfbushansdfbushansldfbusdlbudasdfbushaasdfsfbushan", "bushan"));
    }
    public static int numberOfOccurences(String string, String target){
        int length = target.length();
        int count = 0;
        int index = 0;
        while(index != -1){
            index = string.indexOf(target, index);
            
            if(index != -1){
                count++;
                index += length;
            }
        }
        return count;
    }
}