Friday, August 23, 2013

Coderbyte: Vowel Count (Ruby)

PROBLEM

Have the function VowelCount(str) take the str string parameter being passed and return the number of vowels the string contains (ie. "All cows eat grass" would return 5). Do not count y as a vowel for this challenge.

SOLUTION

def VowelCount(str) 
    vowel_count = 0
    str.split("").each { |letter| vowel_count += 1 if letter =~ /[aeiouAEIOU]/ }
    return vowel_count
end

print VowelCount(STDIN.gets)