Hi everybody,
I’m stuck with Acronym exercise in Kotlin. I have made this code
object Acronym {
var acronym = ""
var nextLetter= ' '
var charA = 'a'
fun generate(phrase: String) : String {
acronym = " "
// Letter after hyphen(-)
for((index,value) in phrase.withIndex()) {
if (value == '-') {
// Get the Next value after hyphen
nextLetter = phrase[index + 1]
if(nextLetter.isLowerCase()) {
acronym += nextLetter.uppercase()
}
// Clear nextLetter
nextLetter = ' '
}
// Letter after space
else if(value == ' ') {
nextLetter = phrase[index + 1]
if(nextLetter.isLowerCase()) {
acronym += nextLetter.uppercase()
}
// Clear nextLetter
nextLetter = ' '
}
// Capitals
if(value.isUpperCase()) {
//If the previous letter was caps, don't add to acronym
if( index > 0 && phrase[index - 1].isUpperCase()) {
// DO NOTHING
}
else { acronym += value }
}
}
return acronym
}
}
On my computer, it works perfectly and returns me Acronym for all sentences, but in Exercism I can’t validate the test.
I don’t understand why?
Thx in advance. Jack.