Saturday, 22 October 2016

Java Program to Find Sub string Frequency in a Given String


import java.io.*;

class FrequencyCountSubString
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     
        System.out.println("Enter the String: ");
        String s=br.readLine();
        int len1 = s.length();
     
        System.out.println("Enter substring: ");
        String sub=br.readLine();
        int len2 = s.length();

        int ind,count=0;
     
        for(int i=0; i+len1<=len2; i++)  
        {
            ind=s.indexOf(sub,i);

            if(ind>=0)
            {
                count++;
               i=ind;

            }
        }
        System.out.println("Occurence of '"+sub+"' in String is "+count);

    }
}

No comments:

Post a Comment