Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class WordCount {
  4. public static void main(String[]args){
  5.  
  6. Scanner scan=new Scanner(System.in);
  7.  
  8. System.out.println("Enter a string:");
  9. String s=scan.nextLine();
  10.  
  11. String[] words = s.split("[ ':.!?]");
  12.  
  13. String word = "";
  14. int [] count=new int[20];
  15. for (int i=0;i<words.length;i++){
  16.  
  17. word = words[i].toLowerCase();
  18.  
  19. for (int j=0;j<words.length;j++){
  20. // first increment i's count for every
  21. // equal word
  22. if (words[j].toLowerCase().equals(word)){
  23. count[i]++;
  24. // then make sure the other instances of this
  25. // word get a zero count
  26. if (i != j){
  27. count[j] = 0;
  28. }
  29. }
  30. }
  31. }
  32. // correct this loop so that it doesn't
  33. // print the zero counts
  34. for (int y=0;y<words.length;y++){
  35. System.out.println(words[y]+"\t"+count[y]);
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement