Defanged Version of Internet Protocol Address

02 Feb, 2022
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[.]1Input: 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++
Java
Python3
C#
Javascript
// 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 Codeint main(){    string str = "255.100.50.0";    cout << GeberateDefangIP(str);    return 0;}
// Java implementation to find the// defanged version of the IP addressclass 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 Codepublic static void main(String[] args){    String str = "255.100.50.0";         System.out.println(GeberateDefangIP(str));}} // This code is contributed by rutvik_56
# 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 Codestr = "255.100.50.0";print(GeberateDefangIP(str)); # This code is contributed by Nidhi_biet
// C# implementation to find the// defanged version of the IP addressusing 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 Codepublic static void Main(){    string str = "255.100.50.0";         Console.Write(GeberateDefangIP(str));}} // This code is contributed by Code_mech
<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 Codevar str = "255.100.50.0";document.write( GeberateDefangIP(str)); </script>
255[.]100[.]50[.]0
We can improve the above solution by using Regular Expressions
Python3
import redef 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
1[.]1[.]1[.]2
255[.]100[.]50[.]0
rutvik_56
Code_Mech
nidhi_biet
noob2000
pranjalpkp
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
What is Data Structure: Types, Classifications and Applications
Print all the duplicates in the input string
Print all subsequences of a string
A Program to check if strings are rotations of each other or not
String class in Java | Set 1
Find if a string is interleaved of two other strings | DP-33
Remove first and last character of a string in Java
Find the smallest window in a string containing all characters of another string
Program to count occurrence of a given character in a string