From d782a5138b047c6ee14bded264a1af24fbcfcb4d Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Tue, 9 Sep 2025 08:26:34 -0600 Subject: 1108. defanging an ip address --- defanging_ip_address.c | 30 ++++++++++++++++++++++++++++++ defanging_ip_address.py | 22 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 defanging_ip_address.c create mode 100644 defanging_ip_address.py diff --git a/defanging_ip_address.c b/defanging_ip_address.c new file mode 100644 index 0000000..0dc4641 --- /dev/null +++ b/defanging_ip_address.c @@ -0,0 +1,30 @@ +// 1108. Defanging an IP Address +#include "leetcode.h" + +/* + * given a valid ipv4 'address', return a defanged version of that ip address. a + * defanged ip address replaces every period "." with '[.]'. + */ + +char *defangIPaddr(char *address) { + char *ip = (char *)calloc(sizeof(char), sizeof("###[.]###[.]###[.]###")); + char *ans = ip; + for (char *i = address; *i; i++) { + if (*i == '.') { + *ip++ = '['; + *ip++ = '.'; + *ip++ = ']'; + } else + *ip++ = *i; + } + return ans; +} + +int main() { + char *a1 = "1.1.1.1", *dipa1 = defangIPaddr(a1); + char *a2 = "255.100.50.0", *dipa2 = defangIPaddr(a2); + printf("%s\n", dipa1); // expect: 1[.]1[.]1[.]1 + printf("%s\n", dipa2); // expect: 255[.]100[.]50[.]0 + free(dipa1); + free(dipa2); +} diff --git a/defanging_ip_address.py b/defanging_ip_address.py new file mode 100644 index 0000000..c9daae2 --- /dev/null +++ b/defanging_ip_address.py @@ -0,0 +1,22 @@ +# 1108. Defanging an IP Address +from re import sub + +""" +given a valid ipv4 'address', return a defanged version of that ip address. a +defanged ip address replaces every period "." with '[.]'. +""" + + +class Solution(object): + def defangIPaddr(self, address): + """ + :type address: str + :rtype: str + """ + return sub("\.", "[.]", address) + + +if __name__ == "__main__": + obj = Solution() + print(obj.defangIPaddr(address="1.1.1.1")) + print(obj.defangIPaddr(address="255.100.50.0")) -- cgit v1.2.3