Created
October 28, 2015 19:59
-
-
Save vdonchev/f56154f99dd173e4d525 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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