Pangram issues

Hey guys, I am trying to write a code that will check if the sentence is a pangram.
Any hints what am I doing wrong?

fun main(args: Array){
val string = (“tralalala”)
fun ispangram(lowerstring: String): Boolean{
val lowerstring = string.map {it.toLowerCase()}
return (‘a’…‘z’).all {lowerstring.contains(it)};
}
}

I’ve created some random string.
Then a function that first changes all elements to lower case.
At the end I wanted to get true/false so I used .all to scan if ‘lowerstring’ contains the elements from a to z.
Am I even close?
Thanks for your time :slight_smile:

This code should work fine. There are some formating errors, but I think they might be due to you positing this here. The one problem you have is that you never call ispangram. I cleaned up the code a little bit. It should work:

fun main(){
    var string = "tralalala"
    println("${isPangram(string)} $string")
    string = "abcdefghijklmnopqrstuvwxyz"
    println("${isPangram(string)} $string")
}


fun isPangram(input: String): Boolean{
	val lowerstring = input.map {it.toLowerCase()}
	return ('a'..'z').all {lowerstring.contains(it)}
}

Also you have isPangram as an inner function in main. This is ok, but you are using string instead of lowerstring as your input. string gets captured from the main function so either use the parameter as your input (see my cleaned version) or you can remove the parameter completely.

fun main() {
    val string = "tralalala"
    fun isPangram(): Boolean{
	val lowerstring = string.map {it.toLowerCase()}
	return ('a'..'z').all {lowerstring.contains(it)}
    }
    println("${isPangram()} $string")
}
1 Like

Beautiful thanks for help!

If you indent you code blocks with 4 spaces or use tripple backticks (```) before and after the block, your code will be rendered as such.

You can also create runnable and editable examples using ```run-kotlin <CODE HERE> ``` like this:

fun main() {
    val numbers = listOf(3, 1, 5, 4, 2)

    // Try editing me!
    println(numbers.sorted())
}

Here’s a link to the blog post about it

As further tweaks:

You don’t need to convert each character with string.map {it.toLowerCase()}; you can simply call string.toLowerCase() (which will be more efficient as well as more concise).

And an alternative to { lowerstring.contains(it) } is { it in lowerstring }. (They mean precisely the same, but the latter is usually a little easier to read.)

2 Likes