Handling strings

Report a typo

The function printFifthCharacter represented below returns the 5th character of the input string. However, it does not allow the possibility to have less than 5 characters in the entered word. In such cases, the StringIndexOutOfBoundsException will be thrown.

Write code in the printFifthCharacter function that in the case of such an exception will return the string The input word is too short! and will not interrupt the execution of the program.

Use the try/catch statement.

Sample Input 1:

Bumblebee

Sample Output 1:

The fifth character of the entered word is l

Sample Input 2:

155

Sample Output 2:

The input word is too short!

Sample Input 3:

Kotlin

Sample Output 3:

The fifth character of the entered word is i
Write a program in Kotlin
fun printFifthCharacter(input: String): String {
return "The fifth character of the entered word is ${input[4]}"
}
___

Create a free account to access the full topic