Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int wordCount(String s1)
- {
- String check = new String();
- int counter = 0;
- 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
- {
- check = this.str.substring(i,(i+s1.length())); //check = a three letter substring ; changes every iteration
- if(check.equals(s1)) //checks equality
- {
- 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
- {
- counter++; //increments counter
- }
- }
- }
- return counter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement