This is a basic homework problem that I was assigned and I seem to not be understanding how to do it from a logical standpoint. This last problem is to search for a keyword within a string of lowercase characters.
This is the last problem.
- Problem 2
- Assume s is a string of lower case characters and a search keyword.
- Write a program that prints the number of times the search keyword occurs in s.
Example:
- Given s = 'azcbobobegghakl' and the search keyword is 'bob'
- Your program should print Number of times bob occurs is: 2
I have been able to make the program using a javascript method before to search for a keyword but it only returned 1 occurrence of 'bob'. The problem I am having is the logic to understand how to solve this question. If this was an array of strings or if it was a string with spaces so that the words would be separated then I understand how to do that. But this just confuses me.
This only returns 1 occurrence of 'bob' from string 'azcbobobegghakl' but the professor wants it to return 2 occurrences.
function searchForKeyWord(str, keyword) {
return str.match(keyword).length;
}