0
0
RubyProgramBeginner · 2 min read

Ruby Program to Count Vowels and Consonants

Use a Ruby program that reads a string, then counts vowels with vowels = str.count('aeiouAEIOU') and consonants with consonants = str.count('a-zA-Z') - vowels to get the counts.
📋

Examples

Inputhello
OutputVowels: 2, Consonants: 3
InputRuby Programming
OutputVowels: 4, Consonants: 11
Input12345!@#
OutputVowels: 0, Consonants: 0
🧠

How to Think About It

To count vowels and consonants, first identify vowels as letters a, e, i, o, u (both uppercase and lowercase). Count how many vowels appear in the string. Then count all alphabet letters and subtract the vowel count to get consonants. Ignore numbers, spaces, and symbols.
📐

Algorithm

1
Get input string from the user.
2
Count vowels by checking each character if it is a vowel.
3
Count all alphabetic characters in the string.
4
Calculate consonants by subtracting vowel count from total alphabet count.
5
Print the counts of vowels and consonants.
💻

Code

ruby
puts 'Enter a string:'
str = gets.chomp
vowels = str.count('aeiouAEIOU')
alphabets = str.count('a-zA-Z')
consonants = alphabets - vowels
puts "Vowels: #{vowels}, Consonants: #{consonants}"
Output
Enter a string: hello Vowels: 2, Consonants: 3
🔍

Dry Run

Let's trace the input 'hello' through the code

1

Input string

User inputs 'hello'

2

Count vowels

'hello'.count('aeiouAEIOU') = 2 (e, o)

3

Count alphabets

'hello'.count('a-zA-Z') = 5 (h, e, l, l, o)

4

Calculate consonants

5 - 2 = 3 consonants (h, l, l)

5

Print result

Output: 'Vowels: 2, Consonants: 3'

StepOperationValue
2Count vowels2
3Count alphabets5
4Calculate consonants3
💡

Why This Works

Step 1: Counting vowels

The count method with 'aeiouAEIOU' counts all vowels ignoring case.

Step 2: Counting alphabets

Counting 'a-zA-Z' finds all letters, including vowels and consonants.

Step 3: Finding consonants

Subtracting vowel count from total alphabets gives consonant count.

🔄

Alternative Approaches

Iterate characters and check vowels/consonants
ruby
puts 'Enter a string:'
str = gets.chomp
vowels = 0
consonants = 0
str.each_char do |ch|
  if ch =~ /[aeiouAEIOU]/
    vowels += 1
  elsif ch =~ /[a-zA-Z]/
    consonants += 1
  end
end
puts "Vowels: #{vowels}, Consonants: #{consonants}"
This method is more flexible for adding rules but slower for long strings.
Use regular expressions to scan vowels and consonants
ruby
puts 'Enter a string:'
str = gets.chomp
vowels = str.scan(/[aeiouAEIOU]/).size
consonants = str.scan(/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]/).size
puts "Vowels: #{vowels}, Consonants: #{consonants}"
Using regex scan is clear and easy to customize but may be less efficient.

Complexity: O(n) time, O(1) space

Time Complexity

The program scans the string once to count vowels and consonants, so it runs in linear time relative to string length.

Space Complexity

It uses a fixed number of counters and no extra data structures, so space is constant.

Which Approach is Fastest?

Using count is fastest for simple counting, while iteration or regex scanning is more flexible but slightly slower.

ApproachTimeSpaceBest For
Using count methodO(n)O(1)Simple and fast counting
Iterate charactersO(n)O(1)Flexible rules and conditions
Regex scanO(n)O(1)Clear pattern matching, customizable
💡
Use count with character sets to quickly count vowels and consonants in Ruby.
⚠️
Beginners often forget to count only alphabetic characters and include digits or symbols as consonants.