Advertisement
dimipan80

6. Count Specified Word

Sep 12th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. /* Write a program to find how many times a word appears in given text.
  2.  * The text is given at the first input line.
  3.  * The target word is given at the second input line.
  4.  * The output is an integer number. Please ignore the character casing.
  5.  * Consider that any non-letter character is a word separator. */
  6.  
  7. import java.util.Scanner;
  8.  
  9. public class _06_CountSpecifiedWord {
  10.  
  11.     public static void main(String[] args) {
  12.         // TODO Auto-generated method stub
  13.         Scanner scan = new Scanner(System.in);
  14.         System.out.println("Enter your target Text on single line:");
  15.         String inputText = scan.nextLine();
  16.         System.out.print("Enter your target Word: ");
  17.         String targetStr = scan.next();
  18.  
  19.         String[] words = inputText.toLowerCase().split("[\\W]+");
  20.         targetStr = targetStr.toLowerCase();
  21.         int counter = 0;
  22.         for (String word : words) {
  23.             if (word.equals(targetStr)) {
  24.                 counter++;
  25.             }
  26.         }
  27.  
  28.         System.out.println("Target Word appears in Given Text " + counter
  29.                 + " many times!");
  30.  
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement