Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Number of Vowels in a String
    I tested my code below on an IDE and it worked - it printed the correct number of vowels for any given string. But when I test it in Coderbyte, it always returns 0, the interger value. Am I doing something wrong? def VowelCount(str) total = 0 i = 0 while i < str.length if (str[i] == "a" || str[i] == "e" || str[i] == "i" || str[i] == "o" || str[i] == "u") total += 1 end i += 1 end return total end # keep this function call here # to see how to enter arguments in Ruby scroll down VowelCount(STDIN.gets)
    jchu4483 posted this question on 10/25/15 | ruby, string, vowel
  • +
  • 0
  • -
  • You could use regular expressions. I'm not sure what it would be in Ruby, but in JS it'd look like: function vowelCounter (string){ return string.replace(/[^aeiou]/gi,"").length } That replaces every letter in the string <i>other than</i> a, e, i, o, or u, with "", then returns the new string's length.
    Log in to write an answer.