diff options
author | Jack Sangdahl <[email protected]> | 2025-09-09 08:26:34 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-09-09 08:26:34 -0600 |
commit | d782a5138b047c6ee14bded264a1af24fbcfcb4d (patch) | |
tree | da7211c8c0c03ac7e7fd84574efddc4331613773 | |
parent | d17d14d45956ea9af8f75e23181d708e4f76979f (diff) | |
download | leetcode-d782a5138b047c6ee14bded264a1af24fbcfcb4d.tar.gz leetcode-d782a5138b047c6ee14bded264a1af24fbcfcb4d.zip |
1108. defanging an ip address
-rw-r--r-- | defanging_ip_address.c | 30 | ||||
-rw-r--r-- | defanging_ip_address.py | 22 |
2 files changed, 52 insertions, 0 deletions
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")) |