Defanging an IP Address - Problem
Given a valid IPv4 IP address, you need to "defang" it for security purposes. In web security, displaying IP addresses directly can be a vulnerability, so we often need to make them safe for display.
A defanged IP address replaces every period "." with "[.]" to prevent it from being interpreted as a clickable link or executable code.
Example: The IP address "192.168.1.1" becomes "192[.]168[.]1[.]1"
Goal: Transform the input IP address string by replacing all dots with [.] and return the defanged version.
Input & Output
example_1.py โ Basic IP Address
$
Input:
address = "1.1.1.1"
โบ
Output:
"1[.]1[.]1[.]1"
๐ก Note:
Each dot in the IP address is replaced with [.] to create the defanged version
example_2.py โ Standard IP Address
$
Input:
address = "255.100.50.0"
โบ
Output:
"255[.]100[.]50[.]0"
๐ก Note:
All three dots are replaced with [.], keeping all numeric parts unchanged
example_3.py โ Common Private IP
$
Input:
address = "192.168.1.1"
โบ
Output:
"192[.]168[.]1[.]1"
๐ก Note:
This common private network IP address gets defanged by replacing all dots with [.]
Constraints
-
The given
addressis a valid IPv4 address - IPv4 addresses consist of 4 integers separated by dots
- Each integer is between 0 and 255 inclusive
- The input will always be a valid IP address format
Visualization
Tap to expand
Understanding the Visualization
1
Identify Dots
Scan through the IP address to find all dot characters
2
Replace Safely
Replace each dot with [.] to neutralize its network meaning
3
Preserve Numbers
Keep all numeric parts exactly the same
4
Return Safe String
The result is a defanged IP that won't be interpreted as a network address
Key Takeaway
๐ฏ Key Insight: String replacement is about pattern matching and substitution - identify the target pattern (dots) and replace with the safe pattern ([.])
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code