Q:

In Java, how do I take a string that a user enters and count the number of times 2 specific lowercase letters come up in order?

So like if a string is, "I am going fishing"
So the number of times the lowercase letters "ng" come up in that specific order in this string is 2 times. And they have to be right next to each other, like no space between them.
So how do I do that? Please help. So whatever string a user enters, it must count the number of times "ng" comes up. Please help. Thanks!
JAVA

A:

If you use regex, you only need one line:
int count = input.replaceAll("[^n]|n(?!g)", "").length();

This works by removing (by replacing with a blank), all characters that are either:

not an "n"
an "n" not followed by a "g"

The resulting String will contain one "n" for every "ng" in the original String, so the count is simply its length.

