From the course: Java Practice: Collections

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Solution: Occurrences of a word in a string

Solution: Occurrences of a word in a string - Java Tutorial

From the course: Java Practice: Collections

Solution: Occurrences of a word in a string

- [Instructor] Here is my solution to the challenge where we want to find the number of occurrences of a word in a given source string. And as always, my solution isn't necessarily the best solution. If you came up with a different solution, that's great. Hopefully you can learn something from my solution as well to help you improve your problem solving skills when writing code. So we have our get occurrences method and we have our source string and word. The source string may contain periods, like the ones that we have in this one. So because of that, what I want to start out with doing is removing all the periods. Then we have a list of words separated by spaces. So if we can turn those words into an array, we can see which of those inside of the array match the source word, and then we simply return the count of those words. So let's take a look at how that works in code. So I'm going to delete this comment inside…

Contents