Skip to content
Related Articles

Related Articles

Improve Article
Defanged Version of Internet Protocol Address
  • Difficulty Level : Basic
  • Last Updated : 19 Apr, 2021

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

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :