How do I check a given string starts with a Vowel or Consonant in Kotlin

Published January 17, 2023

In this kotlin program we are going to check a given string is starts with Vowel or Consonant. To check this we just read the first character from the String and then check that character is a Vowel or Consonant. In this example we are reading the user input using readLine() method and then we are converting this string to charArray using  toCharArray() method, then take the first index of the char array then by using kotlin if conditions we are check that char is a Vowel or Consonant.

Functions used in this example

  • readLine()
  • toCharArray()
  • Array method contains()

 

Example code to check the Character is a Vowel or consonant

fun main()
{
    println("Enter Enter a String:")
    val userInput = readLine()

    // val charToCheck = 'e'

    // Comment below line and uncomment above line if you do not want to take input from user.
    val charToCheck = userInput!!.toCharArray()[0]      // Interested only in first character.

    val result = if (
        (charToCheck == 'a')
        || (charToCheck == 'e')
        || (charToCheck == 'i')
        || (charToCheck == 'o')
        || (charToCheck == 'u')
        || (charToCheck == 'A')
        || (charToCheck == 'E')
        || (charToCheck == 'I')
        || (charToCheck == 'O')
        || (charToCheck == 'U')
    )
        "Vowel"
    else
        "Consonant"

    println("Character ($charToCheck) is $result")

}

 

Code Explanation:

In this first, we read value from the user using readLine() method and stored value in userInput variable.
Then, we are converting this values into character arrays and storing the result into charToCheck variable.
Then, using if-else statement to check character is vowel or consonant. If the character is vowel, if-else block returns “Vowel”, otherwise, it returns “Consonant”.

 

Sample Output :

Enter a String:
Kotlin Tutorial
Character (K) is Consonant

 

Check Vowel / Consonant Using When block in kotlin

The above same example we can implement using kotlin "when" block

fun main()
{
    println("Enter a String:")
    val userInput = readLine()

    // val charToCheck = 'e'

    // Comment below line and uncomment above line if you do not want to take input from user.
    val charToCheck = userInput!!.toCharArray()[0]      // Interested only in first character.

    var result = "Consonant"
    when(charToCheck) {
        'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' -> result = "Vowel"
    }

    println("Character ($charToCheck) is $result")


}

 

Sample Output

Enter a String:
A Kotlin Tutorial
Character (A) is Vowel

 

Check Vowel / Consonant Using charArray contains method

In this way we can also check the character is a Vowel or Consonant. For this we will add all vowels in a charArray then using contains() method we will check the character is inside Vowel array or not. array contains that char then it will be a Vowel otherwise it will be a Consonant.

fun main()
{
    println("Enter a String:")
    val userInput = readLine()

    // val charToCheck = 'e'

    // Comment below line and uncomment above line if you do not want to take input from user.
    val charToCheck = userInput!!.toCharArray()[0]      // Interested only in first character.

    val vowelChars = arrayOf('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')

    val result = if(vowelChars.contains(charToCheck)) "Vowel" else "Consonant"

    println("Character ($charToCheck) is $result")



}

 

Sample Output:

Enter a String:
Kotlin Tutorial
Character (K) is Consonant

 

If we were not entered any String, then above all examples will return an "ArrayIndexOutOfBoundsException" error

Enter a String:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at com.example.kotlinsamples.KotlinCharKt.main(KotlinChar.kt:11)
    at com.example.kotlinsamples.KotlinCharKt.main(KotlinChar.kt)

 

To fix this error/exception we need to check the given string is empty or not. 

val userInput = readLine()

// val charToCheck = 'e'
if(userInput!!.isEmpty())
{
    println("Please enter a String to check a character is a Vowel or Cosonant")
    return;
}

 

If we enter empty string then it will print the below output on the user console

Enter a String:

Please enter a String to check a character is a Vowel or Cosonant

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

74 Views