Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to find how many times a word appears in given text.
- * The text is given at the first input line.
- * The target word is given at the second input line.
- * The output is an integer number. Please ignore the character casing.
- * Consider that any non-letter character is a word separator. */
- import java.util.Scanner;
- public class _06_CountSpecifiedWord {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter your target Text on single line:");
- String inputText = scan.nextLine();
- System.out.print("Enter your target Word: ");
- String targetStr = scan.next();
- String[] words = inputText.toLowerCase().split("[\\W]+");
- targetStr = targetStr.toLowerCase();
- int counter = 0;
- for (String word : words) {
- if (word.equals(targetStr)) {
- counter++;
- }
- }
- System.out.println("Target Word appears in Given Text " + counter
- + " many times!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement