Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. public int wordCount(String s1)
  2. {
  3. String check = new String();
  4. int counter = 0;
  5. for (int i = 0; i <this.str.length() - (s1.length() +1); i++) //iterates through big strings length - little strings length so little string has room to iterate
  6. {
  7. check = this.str.substring(i,(i+s1.length())); //check = a three letter substring ; changes every iteration
  8. if(check.equals(s1)) //checks equality
  9. {
  10. if ((this.str.charAt(s1.length() +1) == ' ' || this.str.charAt(s1.length() +1) == '.') && (this.str.charAt(i-1) == ' ' || i == 0)) //checks if there is a space afterwards or a period afterwards AND whether there is a space before or it is the first word
  11. {
  12. counter++; //increments counter
  13. }
  14. }
  15. }
  16. return counter;
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement