Count vowels in a string (in Ruby)

First of all, it is helpful to know that there are five vowels in English alphabet :) these are a, e, i, o & u. Sometimes y is counted as a sixth vowel, but according to our task we should treat it as a consonant.

# Write a method that takes a string and returns the number of vowels
# in the string. You may assume that all the letters are lower cased.
# You can treat "y" as a consonant.

Maybe the first solution I came up with is not the most elegant one, but I am a beginner in Ruby and this way felt self-explanatory to me. First, we need to assign a value of zero to a variable ‘vowels’, it reflects the number of vowels in a string in the beginning of the iteration. Then I declare a variable ‘counter’ with a value of zero as well and use a while loop which goes through all characters of the selected string.

string[counter] #returns a character with an index ‘counter’, #counter = 0 => string[0] etc.

Then we add an if statement and several Logical OR Operators || and check if string[counter] equals one of the vowels. If it is the case, each time a character equals a vowel, we increase a variable ‘vowels’ by 1.

if string[counter]=="a" || string[counter]=="e" etc.
vowels += 1
end

Then we continue iterating through characters in our string, incrementing ‘counter’ variable by 1 each time. When we reach the length of the string, we return the ‘vowels’ variable, which is a sum of all vowels in the string.

All in one piece:

def count_vowels(string)
vowels = 0
counter = 0
while counter < string.length do
if string[counter]=="a" || string[counter]=="e" || string[counter]=="i" || string[counter]=="o" || string[counter]=="u"
vowels += 1
end
counter += 1
end
return vowels
end
count_vowels("La la Land i like a lot")
#output would be
#=> 8

So, we are good for now. Just to test once again, what I’ve learned, let’s assume the task was to count the consonants instead. It would have been absolutely inefficient to check if a character is equal to one of 21 consonants, writing everything out. But it can be handy to use a Logical NOT Operator ! and in this case we want that all of the if statements to be true at the same time and use Logical AND Operator && instead of ||. One trick is to add one additional condition, which is to check if character is also not empty:

&& string[counter]!=" " # you can also check for commas != ","

We would only need to slightly rewrite our method and now it counts consonants:

def count_consonants(string)
consonants = 0
counter = 0
while counter < string.length do
if string[counter]!="a" && string[counter]!="e" && string[counter]!="i" && string[counter]!="o" && string[counter]!="u" && string[counter]!=" "
puts consonants += 1
end
counter += 1
end
return consonants
end
count_consonants("La la Land i like a lot")
#output would be
#=> 9

Okay, now we know that our string contains 8 vowels and 9 consonants.

In the meantime I have come up with another solution by employing methods I used already .split for converting strings into arrays and .each to return all the elements of an array.

  • We assign an array which contains our vowels to the variable ‘vowels’ already in the beginning.
  • Than we do a double iteration: within a ‘sentence’ (prior converting it to an array) and within a ‘vowels’ array.
  • Our approach is to compare if a character in the ‘sentence’ array equals to one of the vowels from the ‘vowels’ array.
  • If yes, we increase a counter variable by 1 each time.
  • In the end we print the number of vowels
def count_vowels(sentence)
vowels = ["a", "e", "i", "o", "u"] #the same as %w[ a e i o u ]
counter = 0
sentence.split("").each do |char|
vowels.each do |vowel|
if char == vowel
counter += 1
end
end
end
puts counter
end
count_vowels("La la land i like a lot")

Done :)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.