0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Check Vowel or Consonant

In Kotlin, you can check if a character is a vowel or consonant by using when with conditions like char.lowercaseChar() in listOf('a','e','i','o','u') to identify vowels and treat others as consonants.
📋

Examples

Inputa
Outputa is a vowel
InputB
OutputB is a consonant
Input1
Output1 is not an alphabet
🧠

How to Think About It

To decide if a character is a vowel or consonant, first check if it is a letter. If not, say it's not an alphabet. If it is a letter, convert it to lowercase and check if it matches any vowel letters 'a', 'e', 'i', 'o', or 'u'. If yes, it's a vowel; otherwise, it's a consonant.
📐

Algorithm

1
Get the input character from the user.
2
Check if the character is a letter.
3
If not a letter, print it is not an alphabet.
4
If it is a letter, convert it to lowercase.
5
Check if the lowercase character is in the set of vowels (a, e, i, o, u).
6
Print that the character is a vowel if yes, else print it is a consonant.
💻

Code

kotlin
fun main() {
    print("Enter a character: ")
    val ch = readLine()?.firstOrNull() ?: return
    if (!ch.isLetter()) {
        println("$ch is not an alphabet")
    } else {
        when (ch.lowercaseChar()) {
            'a', 'e', 'i', 'o', 'u' -> println("$ch is a vowel")
            else -> println("$ch is a consonant")
        }
    }
}
Output
Enter a character: a a is a vowel
🔍

Dry Run

Let's trace input 'B' through the code

1

Read input

User inputs 'B', stored in variable ch = 'B'

2

Check if letter

'B'.isLetter() returns true

3

Convert to lowercase

'B'.lowercaseChar() is 'b'

4

Check vowel list

'b' is not in ('a','e','i','o','u')

5

Print result

Print 'B is a consonant'

StepCharacterIs Letter?LowercaseIs Vowel?Output
1BtruebfalseB is a consonant
💡

Why This Works

Step 1: Check if input is a letter

Using isLetter() ensures the program only processes alphabets, ignoring digits or symbols.

Step 2: Normalize case

Converting to lowercase with lowercaseChar() simplifies vowel checking by handling both uppercase and lowercase inputs uniformly.

Step 3: Use when for vowel check

The when expression matches the character against vowels and prints the correct result, making the code clear and concise.

🔄

Alternative Approaches

Using if-else instead of when
kotlin
fun main() {
    print("Enter a character: ")
    val ch = readLine()?.firstOrNull() ?: return
    if (!ch.isLetter()) {
        println("$ch is not an alphabet")
    } else {
        val c = ch.lowercaseChar()
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            println("$ch is a vowel")
        } else {
            println("$ch is a consonant")
        }
    }
}
This approach uses simple if-else checks but is slightly longer and less readable than when.
Using a set to check vowels
kotlin
fun main() {
    val vowels = setOf('a', 'e', 'i', 'o', 'u')
    print("Enter a character: ")
    val ch = readLine()?.firstOrNull() ?: return
    if (!ch.isLetter()) {
        println("$ch is not an alphabet")
    } else if (vowels.contains(ch.lowercaseChar())) {
        println("$ch is a vowel")
    } else {
        println("$ch is a consonant")
    }
}
Using a set makes it easy to add or remove vowels and can improve readability.

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

Time Complexity

The program performs a fixed number of checks regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables and a small set of vowels are stored, so space usage is constant O(1).

Which Approach is Fastest?

All approaches run in constant time; using a set may be slightly more readable but has similar performance to when or if-else.

ApproachTimeSpaceBest For
when expressionO(1)O(1)Clear and concise code
if-else statementsO(1)O(1)Simple logic, easy to understand
Set membership checkO(1)O(1)Easy to modify vowel list
💡
Always convert the input character to lowercase before checking vowels to handle both uppercase and lowercase inputs easily.
⚠️
Beginners often forget to check if the input is a letter and try to classify digits or symbols as vowels or consonants.