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
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")
}
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.)