Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class WordCount {
- public static void main(String[]args){
- Scanner scan=new Scanner(System.in);
- System.out.println("Enter a string:");
- String s=scan.nextLine();
- String[] words = s.split("[ ':.!?]");
- String word = "";
- int [] count=new int[20];
- for (int i=0;i<words.length;i++){
- word = words[i].toLowerCase();
- for (int j=0;j<words.length;j++){
- // first increment i's count for every
- // equal word
- if (words[j].toLowerCase().equals(word)){
- count[i]++;
- // then make sure the other instances of this
- // word get a zero count
- if (i != j){
- count[j] = 0;
- }
- }
- }
- }
- // correct this loop so that it doesn't
- // print the zero counts
- for (int y=0;y<words.length;y++){
- System.out.println(words[y]+"\t"+count[y]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement