[SOLVED] Making sure every Alphabet is in a string (Kotlin)

Issue

So I have a question where I am checking if a string has every letter of the alphabet in it. I was able to check if there is alphabet in the string, but I’m not sure how to check if there is EVERY alphabet in said string. Here’s the code

fun isPangram (pangram: Array<String>) : String {
    var panString : String
    var outcome = ""

    for (i in pangram.indices){
        panString = pangram[i]

        if (panString.matches(".^*[a-z].*".toRegex())){
            outcome = outcome.plus('1')
        }
        else {outcome = outcome.plus('0')}

    }
    return outcome

    }

Any ideas are welcomed Thanks.

Solution

I think it would be easier to check if all members of the alphabet range are in each string than to use Regex:

fun isPangram(pangram: Array<String>): String =
    pangram.joinToString("") { inputString ->
        when {
            ('a'..'z').all { it in inputString.lowercase() } -> "1"
            else -> "0"
        }
    }

Answered By – Tenfour04

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *