Saturday, December 7, 2013

Java Program to find the number of occurences of a word in a string

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class WordOccurence {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the  String ");
        String first = reader.readLine();
        System.out.println(" Enter the Word to be searched");
        String second = reader.readLine();
        int count = 0;

        String[] array = first.split(" ");
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(second))
                ++count;

        }

        System.out.println(" No. Of Occurences  " + count);
    }
}

No comments:

Post a Comment