Log events

Report a typo

Suppose we have a toy utility method to capitalize strings:

fun String?.capitalize(): String? {
    return when {
        isNullOrBlank() -> this
        length == 1 -> uppercase()
        else -> this[0].uppercase() + substring(1)
    }
}

Your task is to add some logging to this method so it prints the input string both before capitalization and after capitalization in the following format:

Before: string
After: String

where string is an example of the input string. As you can see, the method has three execution paths and they all must be logged.

Sample Input 1:

string

Sample Output 1:

Before: string
After: String

Sample Input 2:

Capitalized

Sample Output 2:

Before: Capitalized
After: Capitalized
Write a program in Kotlin
fun String?.capitalize(): String? {
return when {
isNullOrBlank() -> this
length == 1 -> uppercase()
else -> this[0].uppercase() + substring(1)
}
}
___

Create a free account to access the full topic