Camel Case 4

  • + 0 comments

    Kotlin Solution:

    import java.io.*
    import java.util.*
    /**
    
    **/ 
    
    fun split(list: Array<String>) : String{
        val stringList = arrayListOf<String>()
        var currentString = ""
        list[1].toCharArray().forEach {
            if (it.isUpperCase()) {
                if(currentString.isNotBlank()) {
                    stringList.add(currentString)
                }
                currentString = ""
                currentString += it.lowercaseChar()
            } else {
                currentString += it
            }
        }
        stringList.add(currentString.removeSuffix("()"))
        return stringList.joinToString(" ")
    }
    
    fun combine(list: Array<String>) : String {
        val textToJoin = list[1].split(' ').toMutableList()
    
        when(list[0]){
            "M" -> {
                textToJoin.add("()")
            }
            "C" -> {
                textToJoin[0] = textToJoin[0].replaceFirstChar {
                    it.uppercase()
                }
            }
        }
        for(index in 1 until textToJoin.size) {
            textToJoin[index] = textToJoin[index].replaceFirstChar {
                it.uppercase()
            }
        }
        return textToJoin.joinToString("")
    }
    
    fun main(args: Array<String>) {
        val br = BufferedReader(InputStreamReader(System.`in`))
    
        while(br.ready()) {
            val list = br.readLine().split(';')
            val newString = when {
                list.first() == "S" -> {
                    split(list.slice(1..2).toTypedArray())
                }
    
                list.first() == "C" -> {
                    combine(list.slice(1..2).toTypedArray())
                }
    
                else -> ""
            }
            println(newString)
        }
    }
    

    Had some trouble with the InputStreamReader, but after that everything went smoothly