Write a Kotlin Program to Check Whether a Character is Alphabet or Not

In Kotlin, checking whether a character is an alphabet or not is a straightforward process. Here’s how you can write a program to do it:

fun isAlphabet(char: Char): Boolean {
return char in 'a'..'z' || char in 'A'..'Z'
}

The above function takes a single argument, which is the character that we want to check.

It then returns a Boolean value indicating whether the character is an alphabet or not.

To check whether a character is an alphabet, we use the in operator to check if the character is in the range of lowercase or uppercase letters.

If the character is in either range, then it is considered an alphabet and the function returns true. Otherwise, it returns false.

Here’s an example of how to use this function:

fun main() {
    val char1 = 'a'
    val char2 = '!'
    val char3 = 'Z'

    println("$char1 is alphabet: ${isAlphabet(char1)}") // true
    println("$char2 is alphabet: ${isAlphabet(char2)}") // false
    println("$char3 is alphabet: ${isAlphabet(char3)}") // true
}

In the above example, we have three characters: ‘a’, ‘!’, and ‘Z’.

We pass each character to the isAlphabet() function and print out the result using string interpolation.

The output of the program would be:

a is alphabet: true
! is alphabet: false
Z is alphabet: true

And that’s it! This is a simple and concise way to check whether a character is an alphabet or not in Kotlin.