Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Write a function that concatenates three strings into one using a special separator. If the separator is not specified, it is a single space.

Suppose you have a standard input.

Concatenate the letters 1, 2, and 3. Insert a fourth input separator between the characters in 1-2-3.

However, if a specific keyword is entered, it will be output as a half-width space.

The following is my code.

import java.util.*

fun main() {
    val scanner = Scanner(System.`in`)
    val input = Array(4){scanner.next()}

    if (input[3] == "NO SEPARATOR"){
        println(" ")
    }else{
        input[3] == input[3]
    }
    println("$input[0]$input[3]$input[1]$input[3]$input[2]")
}
//Sample Input

abc
def
ghi
NO SEPARATOR

Sample Output

abc def ghi

I can't think of a solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
436 views
Welcome To Ask or Share your Answers For Others

1 Answer

First of all, you should be reading based off lines, which means you don't need a scanner, you can use readLine. Secondly, you can use joinToString for adding a separator in between:

fun main() {
    val input = Array(3) { readLine()!! }
    var sep = readLine()!!
    if (sep == "NO SEPARATOR") {
        sep = " "
    }
    println(input.joinToString(sep))
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...