In this post, we will go through Kotlin Program to Check if Character is Vowel or Consonant.
Some functions/Methods used in this program –
readLine() : It reads value entered by user before line break.toCharArray() : It converts values to character array.contains : It checks if array contains a given value or not.
1. Check Vowel / Consonant Using if / else statement in kotlin
In this example, we directly type necessary character check in if-else block.
Sourcecode –
fun main() { println("Enter an Alphabet:") val enteredVal = readLine() // val charToCheck = 'e' // Comment below line and uncomment above line if you do not want to take input from user. val charToCheck = enteredVal!!.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") }
When you run the program, output will be
Enter an Alphabet: u Character (u) is Vowel
Explanation:
Here, we read value using
Then, we are converting entered values into character arrays and storing the result into charToCheck variable.
Then, using if-else statement to check character is vowel or consonant.
If character is vowel, if-else block returns “Vowel”, otherwise, it returns “Consonant”. We stored the returned value in
result variable.
Finally, we print result using println() method.
2. Check Vowel / Consonant, Store values in array
In this example, we store necessary character to check in an array.
fun main() { println("Enter an Alphabet:") val enteredVal = readLine() // val charToCheck = 'e' // Comment below line and uncomment above line if you do not want to take input from user. val charToCheck = enteredVal!!.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") }
When you run the program, output will be
Enter an Alphabet: u Character (u) is Vowel
In this example, we store the necessary character to check in an array – vowelChars
Then , we use vowelChars.contains(charToCheck) method to check if array – vowelChars
contains entered character or not. If yes, it is vowel. otherwise, it’s consonant.
Finally, we print result using println() function.
3. Check Vowel / Consonant Using When block in kotlin
Sourcecode –
fun main() { println("Enter an Alphabet:") val enteredVal = readLine() // val charToCheck = 'e' // Comment below line and uncomment above line if you do not want to take input from user. val charToCheck = enteredVal!!.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") }
When you run the program, output will be –
Enter an Alphabet: r Character (r) is Consonant
Here, enteredVal!!.toCharArray()[0] means we are converting values, entered by user, into character array.
Then, taking first character to check if it is vowel or not.
Using when block to check if entered character is vowel or consonant.
If you do press enter button without entering any value, you will get below error –
Enter an Alphabet: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 // Other details...
This is because of enteredVal!!.toCharArray()[0].
Thus, we went through Kotlin Program to Check if Character is Vowel or Consonant.