Skip to content
Related Articles

Related Articles

Defanged Version of Internet Protocol Address
  • Difficulty Level : Basic
  • Last Updated : 09 Jun, 2020

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :