Kotlin – Pangram String

Kotlin – Check if given String is a Pangram String

In Kotlin, a pangram string is a sentence or phrase that contains every letter of the alphabet at least once.

In this tutorial, we will write a function that returns true or false based on whether the given string is a pangram or not respectively, and use the function in an example program.

Examples

Example 1: Check if a String is a Pangram

Let’s start with a simple example of checking if a given string is a pangram.

We’ll create a function isPangram() that takes a string as input and checks if it’s a pangram.

Kotlin Program

fun isPangram(input: String): Boolean {
    val alphabet = "abcdefghijklmnopqrstuvwxyz"
    val inputLowerCase = input.lowercase()

    for (char in alphabet) {
        if (char !in inputLowerCase) {
            return false
        }
    }
    return true
}

fun main() {
    // Test the function with a sample string
    val sampleString = "The quick brown fox jumps over the lazy dog"
    val isPangramResult = isPangram(sampleString)

    if (isPangramResult) {
        println("The string is a pangram.")
    } else {
        println("The string is not a pangram.")
    }
}

Output

The string is a pangram.

Summary

A pangram string in Kotlin is a sentence or phrase that includes all the letters of the alphabet at least once. You can check if a string is a pangram by iterating through the alphabet and checking if each letter is present in the string.