Skip to content

Instantly share code, notes, and snippets.

@vdonchev
Created October 28, 2015 19:59
Show Gist options
  • Save vdonchev/f56154f99dd173e4d525 to your computer and use it in GitHub Desktop.
Save vdonchev/f56154f99dd173e4d525 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountSpecifiedWord {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Insert text (haystack) = ");
String hayStack = scanner.nextLine().toLowerCase();
System.out.print("Insert needle string = ");
String needle = scanner.next().toLowerCase();
int counter = 0;
String[] words = hayStack.split("[^a-z]");
for (String word : words) {
if (word.equals(needle)) {
counter++;
}
}
System.out.println(counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment