The toPascalCase
function is intended to convert strings containing spaces or -
to Pascal case.
Below is my code ->
fun toPascalCase(str: String): String {
lateinit var ans: String
for(i in str) {
if(i != ' ' && i != '-') {
ans += i
}
}
return ans
}
fun main() {
toPascalCase("Harsh kumar-singh")
}
I have used the lateinit
keyword before ans
variable. In the for loop I have initialized the ans
variable but still the compiler throws the following error ->
Exception in thread "main" kotlin.UninitializedPropertyAccessException: lateinit property ans has not
been initialized
at FileKt.toPascalCase (File.kt:10)
at FileKt.main (File.kt:18)
at FileKt.main (File.kt:-1)
Please help me with the code. Thank you
Instead of
lateinit var
, you should use a simple var initializing it to an empty string.