Skip to content
Related Articles

Related Articles

Defanged Version of Internet Protocol Address

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 02 Feb, 2022
View Discussion
Improve Article
Save Article

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

We can improve the above solution by using Regular Expressions

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

Output

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!