Skip to content

Instantly share code, notes, and snippets.

@2hanX
Created September 27, 2020 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2hanX/151782067e523c45abe9446d92989d51 to your computer and use it in GitHub Desktop.
Save 2hanX/151782067e523c45abe9446d92989d51 to your computer and use it in GitHub Desktop.
给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 "."。 示例 1: 输入:address = "1.1.1.1" 输出:"1[.]1[.]1[.]1" 示例 2: 输入:address = "255.100.50.0" 输出:"255[.]100[.]50[.]0" 来源:力扣(LeetCode) 链接:https://leetcode-cn.co
#include<iostream>
#include <string.h>
using namespace std;
string defangIPaddr(string address)
{
for (int i = 0; i < address.length(); ++i)
{
if (address[i] == '.')
{
address.replace(i,1,"[.]");
i+=2;
}
}
return address;
}
int main(int argc, char const *argv[])
{
string address;
cin>>address;
string ans;
ans = defangIPaddr(address);
cout<<ans<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment