-4

How to obtain the total occurrence of searched keyword in a sentence. Eg:-If searched keyword is "Hello World",the output should has the sum of total number of occurrences of "Hello","World" and "Hello World".

Thanks.

arun abraham
  • 147
  • 2
  • 12
  • I am not a big fan of questions like "Hey could you do me this please ?". With a bit of research you could find a solution very quickly. Anyway, you can check my answer to help you begin with. – MadJlzz Sep 19 '16 at 12:16
  • There is a utility built into java that can do this for you. See [this post](http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string) to get yourself started – Takarii Sep 19 '16 at 12:48

1 Answers1

0

You could use some Matcher to do that with regular expression.

import java.util.regex.*;

class Test {

    public static void main(String[] args) {
        String inputToTest = "The string that you want to test";
        Pattern pattern = Pattern.compile("<YOU'RE REGEX PATTERN>");
        Matcher  matcher = pattern.matcher(inputToTest);

       int count = 0;
       while (matcher.find())
           count++;

       System.out.println(count);
    }
}

You can do what you want with this. Just try it yourself now.

MadJlzz
  • 767
  • 2
  • 13
  • 35