Fangs and strings
Weekly challenge 272 — 3 June 2024
Week 272: 3 Jun 2024
You are given a valid IPv4 address. Write a script to return the defanged version of the given IP address. A defanged IP address replaces every period “.” with “[.]".
Example 1 Input: $ip = "1.1.1.1" Output: "1[.]1[.]1[.]1" Example 2 Input: $ip = "255.101.1.0" Output: "255[.]101[.]1[.]0"
I wonder why you would want to do that?
There isn't really much to analyse: if we can assume that the input is valid then the answer is barely even one line of Perl.
Of course, we might have been asked to check:
But we weren't, so I didn't.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2024-06-03 use utf8; # Week 272 - task 1 - Defang IP address use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; defang_ip_address('192.168.1.245'); defang_ip_address('0.0.0.0'); defang_ip_address('255.255.255.255'); defang_ip_address('192.168.1.20'); sub defang_ip_address { $_[0] =~ m|(\d+).(\d+).(\d+).(\d+)|; say qq{\nInput: \$ip = "$_[0]"\nOutput: "$1\[.]$2\[.]$3\[.]$4"}; }
Input: $ip = "192.168.1.245" Output: "192[.]168[.]1[.]245" Input: $ip = "0.0.0.0" Output: "0[.]0[.]0[.]0" Input: $ip = "255.255.255.255" Output: "255[.]255[.]255[.]255" Input: $ip = "192.168.1.20" Output: "192[.]168[.]1[.]20"
Any content of this website which has been created by Peter Campbell Smith is in the public domain