I am trying to solve this problem
Given a sentence containing multiple words, find the frequency of a given word in that sentence.
Construct a method named 'find_frequency' which accepts two arguments 'sentence' and 'word', both of which are String objects.
Example: The method, given 'Ruby is The best language in the World' and 'the', should return 2 (comparison should be case-insensitive).
Hint: You can use the method
Array#countto count the frequency of any element in the given array.
Since the comparison should be case-insensitive. I use these code to help:
word = "the"
word_set = []
word.size.times do |i|
word[i] = word[i].upcase
word_set << word
word[i] = word[i].downcase
end
Inside the block every time after upcase method the word does change and does add to the word_set, however when the block finish the word_set just contain the the the
What is the problem?
The tHe thEand word isthe, result should be3, so the code is try to get the set of every case of the word.downcaseboth, the sentence and the word, so you can compare the downcased word against the downcased sentence?