1. Introduction

As developers, we sometimes need to differentiate whether a particular character is a vowel or a consonant, especially when validating user inputs, detecting patterns in text, etc.

In this tutorial, we’ll explore how to check whether a character is a vowel or consonant using Kotlin.

2. Check if a Character Is a Vowel or Consonant

Now, let’s look at different ways we can achieve our goal using Kotlin. Note that we’ll treat a, e, i, o, and u as vowel characters in this tutorial. 

2.1. Using the when() Expression

To check whether a character is a vowel or consonant case-insensitively in Kotlin, we can use a simple when() expression:

fun isVowel(c: Char): Boolean {
    return when (c.lowercaseChar()) {
        'a', 'e', 'i', 'o', 'u' -> true
        else -> false
    }
}

We can also build off this first check to check for consonants:

fun isConsonant(c: Char): Boolean {
    return !isVowel(c) && c.isLetter()
}

When checking for consonants, we must use the provided isLetter() function after proving that the Char isn’t a vowel.

Now, our test cases look like this:

@Test
fun `using when expression`() {
    assertTrue(isVowel('e'))
    assertTrue(isVowel('I'))
    assertTrue(isVowel('o'))
    assertFalse(isConsonant('o'))
    assertFalse(isVowel('H'))
    assertTrue(isConsonant('H'))
    assertFalse(isVowel('@'))
    assertFalse(isConsonant('@'))
}

2.2. Using a Set

We can equally place all the vowels into a Set and check for the occurrence of a particular character in it:

fun isVowelUsingSet(c: Char): Boolean {
    return c.toLowerCase() in setOf('a', 'e', 'i', 'o', 'u')
}

We can build off of this again to check for consonants:

fun isConsonantUsingSet(c: Char): Boolean {
        return !isVowelUsingMap(c) && c.isLetter()
}

As usual, we need to be sure our method functions properly:

@Test 
fun `using Set method`() { 
    assertTrue(isVowelUsingSet('e'))
    assertTrue(isVowelUsingSet('I'))
    assertTrue(isVowelUsingSet('o'))
    assertFalse(isConsonantUsingSet('o'))
    assertFalse(isVowelUsingSet('H'))
    assertTrue(isConsonantUsingSet('H'))
    assertFalse(isVowelUsingSet('@'))
    assertFalse(isConsonantUsingSet('@'))
}

2.3. Using Regular Expressions

We can also use regular expressions to check if a character is a vowel or consonant. Basically, regular expressions are patterns used to match character combinations in strings, so all we need do is check if a single character matches anything in the set of vowels:

val isVowelLetterRegex = "[AEIOUaeiou]".toRegex()

fun isVowelUsingRegexMethod(c: Char): Boolean {
 return c.toString().matches(isVowelLetterRegex)
}

As usual, we can build up from this vowel check method to check for consonants:

val isLetterRegex = "[a-zA-Z]".toRegex()

fun isConsonantUsingRegex(c: Char): Boolean { val 
    return !isVowelUsingRegexMethod(c) && c.toString().matches(isLetterRegex) 
}

We now test our method as such:

@Test 
fun `using regex method`() { 
    assertTrue(isVowelUsingRegexMethod('e'))
    assertTrue(isVowelUsingRegexMethod('I'))
    assertTrue(isVowelUsingRegexMethod('o'))
    assertFalse(isConsonantUsingRegex('o'))
    assertFalse(isVowelUsingRegexMethod('H'))
    assertTrue(isConsonantUsingRegex('H'))
    assertFalse(isVowelUsingRegexMethod('@'))
    assertFalse(isConsonantUsingRegex('@'))
}

2.4. Using ASCII Values

Sometimes, we must handle a character by its ASCII code instead of the character itself. Therefore, let’s determine how a given ASCII code represents a vowel or consonant character.

One idea is to initialize a set containing all vowel character’s ASCII codes. Then, we can check if the given code in this set:

fun isVowelUsingAsciiValues(asciiCode: Int): Boolean {
    return asciiCode in setOf(65, 69, 73, 79, 85, 97, 101, 105, 111, 117)
}

Let’s check for consonants too based on the above method:

fun isConsonantUsingAscii(asciiCode: Int): Boolean { 
    return (asciiCode in 65..90 || asciiCode in 97..122) &&
        !isVowelUsingAsciiValues(asciiCode) 
}

Finally, let’s test these methods too:

@Test
fun `using ASCII values method`() {
    assertTrue(isVowelUsingAsciiValues('E'.code))
    assertTrue(isVowelUsingAsciiValues('I'.code))
    assertTrue(isVowelUsingAsciiValues('o'.code))
    assertFalse(isConsonantUsingAscii('o'.code))
    assertFalse(isVowelUsingAsciiValues('H'.code))
    assertTrue(isConsonantUsingAscii('H'.code))
    assertFalse(isVowelUsingAsciiValues('@'.code))
    assertFalse(isConsonantUsingAscii('@'.code))
}

3. Conclusion

In this article, we’ve discussed various ways to check if a character is a vowel or consonant in Kotlin. It should be noted that there might be other ways to achieve this same goal, and we should use the most comfortable methods.

As always, the code samples and relevant test cases pertaining to this article can be found over on GitHub.