Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making sure every Alphabet is in a string (Kotlin)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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"
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading