← Coding round practice

Mask a String

EasyStrings

The problem

Mask all but the last 4 characters of a string with asterisks — like showing a saved card or account number in an app.

Sample input

1234567812345678

Sample output

************5678
Show the solution · 8 languages
fun main() {
    val s = "1234567812345678"
    val visible = 4
    println("*".repeat(s.length - visible) + s.takeLast(visible))
}