Open In App

Defanged Version of Internet Protocol Address

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a valid (IPv4) Internet Protocol address S, the task is to find the defanged version of that IP address.
Defanged Version of IP Address: is in which every period “.” is replaced by “[.]”. 

Examples: 

Input: S = “1.1.1.1” 
Output: 1[.]1[.]1[.]1

Input: S = “255.100.50.0” 
Output: 255[.]100[.]50[.]0  

Approach: The idea is to traverse the string and append every character of the string into the final answer string except when the current character is “.”, then append “[.]” into the final answer string.

Below is the implementation of the above approach: 

C++




// C++ implementation to find the
// defanged version of the IP address
 
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to generate a
// defanged version of IP address.
string GeberateDefangIP(string str)
{
    string defangIP = "";
     
    // Loop to iterate over the
    // characters of the string
    for (char c : str)
        (c == '.') ? defangIP += "[.]" :
                     defangIP += c;
    return defangIP;
}
 
// Driven Code
int main()
{
    string str = "255.100.50.0";
    cout << GeberateDefangIP(str);
    return 0;
}


Java




// Java implementation to find the
// defanged version of the IP address
class GFG{
 
// Function to generate a defanged 
// version of IP address.
static String GeberateDefangIP(String str)
{
    String defangIP = "";
     
    // Loop to iterate over the
    // characters of the string
    for(int i = 0; i < str.length(); i++)
    {
       char c = str.charAt(i);
       if(c == '.')
       {
           defangIP += "[.]";
       }
       else
       {
           defangIP += c;
       }
    }
    return defangIP;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "255.100.50.0";
     
    System.out.println(GeberateDefangIP(str));
}
}
 
// This code is contributed by rutvik_56


Python3




# Python3 implementation to find the
# defanged version of the IP address
 
# Function to generate a
# defanged version of IP address.
def GeberateDefangIP(str):
 
    defangIP = "";
     
    # Loop to iterate over the
    # characters of the string
    for c in str:
        if(c == '.'):
            defangIP += "[.]"
        else:
             defangIP += c;
    return defangIP;
 
# Driver Code
str = "255.100.50.0";
print(GeberateDefangIP(str));
 
# This code is contributed by Nidhi_biet


C#




// C# implementation to find the
// defanged version of the IP address
using System;
class GFG{
 
// Function to generate a defanged
// version of IP address.
static String GeberateDefangIP(string str)
{
    string defangIP = "";
     
    // Loop to iterate over the
    // characters of the string
    for(int i = 0; i < str.Length; i++)
    {
    char c = str[i];
    if(c == '.')
    {
        defangIP += "[.]";
    }
    else
    {
        defangIP += c;
    }
    }
    return defangIP;
}
 
// Driver Code
public static void Main()
{
    string str = "255.100.50.0";
     
    Console.Write(GeberateDefangIP(str));
}
}
 
// This code is contributed by Code_mech


Javascript




<script>
 
// Javascript implementation to find the
// defanged version of the IP address
 
// Function to generate a
// defanged version of IP address.
function GeberateDefangIP(str)
{
    var defangIP = "";
     
    // Loop to iterate over the
    // characters of the string
    str.split('').forEach(function(letter) {
        (letter == '.') ? defangIP += "[.]" :
                     defangIP += letter; })
 
    return defangIP;
}
 
// Driven Code
var str = "255.100.50.0";
document.write( GeberateDefangIP(str));
 
</script>


Output

255[.]100[.]50[.]0

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)

We can improve the above solution by using Regular Expressions

C++




// c++ code for the above approach
#include <iostream>
#include <regex>
using namespace std;
 
void Defanged_IP(string Str) {
    regex pattern("\\.");
    string x = regex_replace(Str, pattern, "[.]");
    cout << x << endl;
}
 
// Driver code
int main() {
    string Str = "1.1.1.2";
    Defanged_IP(Str);
    string S = "255.100.50.0";
    Defanged_IP(S);
    return 0;
}
 
// This code is contributed by Prince Kumar


Python3




import re
def Defanged_IP(Str):
    x=re.sub("[.]","[.]",Str)
    print(x)
Str="1.1.1.2"
Defanged_IP(Str)
S = "255.100.50.0"
Defanged_IP(S)
 
#This code is contributed by pranjalpkp


Java




// Java program for the above approach
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Main {
    static void Defanged_IP(String Str) {
        Pattern pattern = Pattern.compile("\\.");
        Matcher matcher = pattern.matcher(Str);
        String x = matcher.replaceAll("[.]");
        System.out.println(x);
    }
     
    // Driver code
    public static void main(String[] args) {
        String Str = "1.1.1.2";
        Defanged_IP(Str);
        String S = "255.100.50.0";
        Defanged_IP(S);
    }
}
 
// This code is contributed by adityashatmfh


Javascript




// Define a function to defang an IP address
function defangedIP(str) {
    // Define a regular expression pattern that matches periods in the IP address
    const pattern = /\./g;
    // Use the `replace` method to replace all matches of the pattern in the IP address with "[.]"
    const x = str.replace(pattern, "[.]");
    // Output the defanged IP address to the console
    console.log(x);
}
 
// Test the `defangedIP` function with two example IP addresses
const str1 = "1.1.1.2";
defangedIP(str1);
const str2 = "255.100.50.0";
defangedIP(str2);


C#




// C# program for the above approach
 
using System;
using System.Text.RegularExpressions;
 
public class Program {
    static void Defanged_IP(String Str) {
        Regex pattern = new Regex("\\.");
        String x = pattern.Replace(Str, "[.]");
        Console.WriteLine(x);
    }
     
    // Driver code
    public static void Main() {
        String Str = "1.1.1.2";
        Defanged_IP(Str);
        String S = "255.100.50.0";
        Defanged_IP(S);
    }
}
 
// This code is contributed by princekumaras


Output

1[.]1[.]1[.]2
255[.]100[.]50[.]0

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads