0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Count Vowels and Consonants

You can count vowels and consonants in Kotlin by looping through each character of a string and checking if it is a vowel using when or if conditions, then incrementing vowel or consonant counters accordingly.
📋

Examples

Inputhello
OutputVowels: 2, Consonants: 3
InputKotlin
OutputVowels: 2, Consonants: 4
Input123!@
OutputVowels: 0, Consonants: 0
🧠

How to Think About It

To count vowels and consonants, read each character in the input string one by one. Check if the character is a letter. If it is, determine if it is a vowel by comparing it to known vowels. If yes, increase the vowel count; if not, increase the consonant count. Ignore non-letter characters.
📐

Algorithm

1
Get the input string from the user.
2
Initialize vowel and consonant counters to zero.
3
For each character in the string:
4
Check if the character is a letter.
5
If it is a vowel, increment vowel counter.
6
Else if it is a consonant, increment consonant counter.
7
Print the counts of vowels and consonants.
💻

Code

kotlin
fun main() {
    val input = readLine() ?: ""
    var vowels = 0
    var consonants = 0
    val vowelsSet = setOf('a', 'e', 'i', 'o', 'u')
    for (char in input.lowercase()) {
        if (char in 'a'..'z') {
            if (char in vowelsSet) vowels++ else consonants++
        }
    }
    println("Vowels: $vowels, Consonants: $consonants")
}
Output
Vowels: 2, Consonants: 4
🔍

Dry Run

Let's trace the input "Kotlin" through the code

1

Input reading

input = "Kotlin"

2

Initialize counters

vowels = 0, consonants = 0

3

Convert to lowercase and iterate

characters: 'k', 'o', 't', 'l', 'i', 'n'

4

Check each character

k: consonant, vowels=0, consonants=1 o: vowel, vowels=1, consonants=1 t: consonant, vowels=1, consonants=2 l: consonant, vowels=1, consonants=3 i: vowel, vowels=2, consonants=3 n: consonant, vowels=2, consonants=4

5

Print result

Vowels: 2, Consonants: 4

CharacterIs Letter?Is Vowel?Vowels CountConsonants Count
kYesNo01
oYesYes11
tYesNo12
lYesNo13
iYesYes23
nYesNo24
💡

Why This Works

Step 1: Check each character

The program looks at each character to see if it is a letter between 'a' and 'z'. This filters out numbers and symbols.

Step 2: Identify vowels

It compares the character to a set of vowels. If it matches, the vowel count increases.

Step 3: Count consonants

If the character is a letter but not a vowel, it counts as a consonant.

Step 4: Output counts

Finally, the program prints how many vowels and consonants were found.

🔄

Alternative Approaches

Using Regex to filter vowels and consonants
kotlin
fun main() {
    val input = readLine() ?: ""
    val vowels = Regex("[aeiouAEIOU]")
    val consonants = Regex("[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]")
    val vowelCount = vowels.findAll(input).count()
    val consonantCount = consonants.findAll(input).count()
    println("Vowels: $vowelCount, Consonants: $consonantCount")
}
This method uses regular expressions to find vowels and consonants, which can be more concise but may be slower for very large strings.
Using filter and count functions
kotlin
fun main() {
    val input = readLine() ?: ""
    val vowelsSet = setOf('a', 'e', 'i', 'o', 'u')
    val lowerInput = input.lowercase()
    val vowelCount = lowerInput.count { it in vowelsSet }
    val consonantCount = lowerInput.count { it in 'a'..'z' && it !in vowelsSet }
    println("Vowels: $vowelCount, Consonants: $consonantCount")
}
This approach uses Kotlin's built-in collection functions for a clean and readable solution.

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

Time Complexity

The program checks each character once, so time grows linearly with input size.

Space Complexity

Only a few counters and a small set of vowels are stored, so space is constant.

Which Approach is Fastest?

The simple loop with set membership is fastest and uses least memory; regex is more expressive but slower.

ApproachTimeSpaceBest For
Loop with set membershipO(n)O(1)Fast and memory efficient
Regex matchingO(n)O(1)Concise but slower for large input
Filter and count functionsO(n)O(1)Readable and idiomatic Kotlin
💡
Convert the input string to lowercase first to simplify vowel checks.
⚠️
Counting non-letter characters as vowels or consonants instead of ignoring them.