-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1108-DefangingAnIPAddress.go
More file actions
45 lines (36 loc) · 1.07 KB
/
1108-DefangingAnIPAddress.go
File metadata and controls
45 lines (36 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
// 1108. 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"
// Output: "1[.]1[.]1[.]1"
// Example 2:
// Input: address = "255.100.50.0"
// Output: "255[.]100[.]50[.]0"
// Constraints:
// The given address is a valid IPv4 address.
import "fmt"
func defangIPaddr(address string) string {
res := []byte{}
for i := 0; i < len(address); i++ {
if address[i] == '.' {
res = append(res, '[')
res = append(res, '.')
res = append(res, ']')
} else {
res = append(res, address[i])
}
}
return string(res)
}
func main() {
// Example 1:
// Input: address = "1.1.1.1"
// Output: "1[.]1[.]1[.]1"
fmt.Println(defangIPaddr("1.1.1.1")) // "1[.]1[.]1[.]1"
// Example 2:
// Input: address = "255.100.50.0"
// Output: "255[.]100[.]50[.]0"
fmt.Println(defangIPaddr("255.100.50.0")) // "255[.]100[.]50[.]0"
}