📜  互联网协议地址的Defanged版本

📅  最后修改于: 2021-09-08 12:44:58             🧑  作者: Mango

给定一个有效的 (IPv4) Internet 协议地址S ,任务是找到该 IP 地址的去污版本。
IP地址的Defanged版本:每个句点“。”替换为“[.]”。
例子:

方法:我们的想法是遍历字符串和字符串的每个字符追加到除非当前字符是最终的答案字符串,然后追加“[]”到最终的答案字符串“”。
下面是上述方法的实现:

C++
// C++ implementation to find the
// defanged version of the IP address
 
#include 
 
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


输出:
255[.]100[.]50[.]0

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live