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
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: |
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: |
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: |
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 |
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 :
|
|
|
|
130 Views |