close

Given a valid (IPv4) IP address, return a defanged version of that IP address.

A defanged IP address replaces every period "." with "[.]".

 

Example 1:

Input: address = "1.1.1.1"
Output: "1[.]1[.]1[.]1"

Example 2:

Input: address = "255.100.50.0"
Output: "255[.]100[.]50[.]0"

 

Constraints:

  • The given address is a valid IPv4 address.

 

 

 

#include <iostream>
#include <string>
using namespace std;


string defangIPaddr(string address)
{
    string result = "";
    for (char & c : address)
    {
        if (c == '.')
            result += "[.]";
        else
            result += c;
    }
    return result;
}

int main()
{
    string str = "192.168.1.1";
    string strResult = defangIPaddr(str);

    cout << strResult << endl;

    getchar();

}

 

 

 

 

 

最近因為在整理一些東西,所以一直都沒有更新BLOG,

特別去寫了一篇Leetcode來刷一下存在感,

我相信八月過後,會有更有趣的內容出來,

大家就敬請期待囉。

 

 

 

arrow
arrow
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()