https://leetcode.com/problems/defanging-an-ip-address/description/
Defanging an IP Address - LeetCode
Can you solve this real interview question? Defanging an IP Address - Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP address replaces every period "." with "[.]". Example 1: Input: address = "1.1.1.1" Outp
leetcode.com
특정 문자가 나오면 변환해서 리턴하는 문제
class Solution {
fun defangIPaddr(address: String): String {
var ans = ""
// for (d in address)
for (i in 0 until address.length) {
if(address[i] == '.') {
ans += "[.]"
} else {
ans += address[i]
}
}
return ans
}
}
for문 도는 방법이 여러개 있는데 알아놔야겠다
val str = "Kotlin!"
for (i in 0..str.length-1) {
println(str[i])
}
//////////
for (i in 0 until str.length) {
println(str[i])
}
////////////
for (i in str.indices) {
println(str[i])
}
//////////
for (c in str) {
println(c)
}
///////////
str.forEach { println(it) }
https://www.techiedelight.com/iterate-over-characters-of-a-string-in-kotlin/
Iterate over characters of a string in Kotlin | Techie Delight
This article explores different ways to iterate over characters of a string in Kotlin... The standard approach to iterate over characters of a string is with index-based for-loop. The idea is to iterate over a range of valid indices with a range expression
www.techiedelight.com
댓글