coding for food

[Ruby] Count E

silveryen 2022. 7. 22. 11:49

Count E

Write a method count_e(word) that takes in a string word and returns the number of e's in the word

def count_e(word)
  count = 0 # use count to track the number of e's

  i = 0 # use i to iterate thru the word
  while i < word.length
    char = word[i]

    if char == "e"
      count += 1
    end

    i += 1
  end

  return count
end

puts count_e("movie") # => 1
puts count_e("excellent") # => 3