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:
address
is a valid IPv4 address.program main
implicit none
character(len=32) :: address
character(len=32) :: defanged_address
! Example 1
address = "1.1.1.1"
call solve(address, defanged_address)
print "(A)", trim(defanged_address)
! Example 2
address = "255.100.50.0"
call solve(address, defanged_address)
print "(A)", trim(defanged_address)
contains
subroutine solve(address, defanged_address)
implicit none
character(len=*), intent(in) :: address
character(len=*), intent(out) :: defanged_address
integer :: i
do i = 1, len(address)
if (address(i:i) == ".") then
defanged_address(i:i+2) = "[.] "
else
defanged_address(i:i) = address(i:i)
end if
end do
end subroutine solve
end program main
1[1[1[1 255[100[50[0
PROGRAM defangIPaddr
IMPLICIT NONE
CHARACTER(len=16) :: address
CHARACTER(len=18) :: defanged_address
address = "1.1.1.1"
defanged_address = defangIPaddr(address)
WRITE (*,*) defanged_address
address = "255.100.50.0"
defanged_address = defangIPaddr(address)
WRITE (*,*) defanged_address
CONTAINS
FUNCTION defangIPaddr(address) RESULT(defanged_address)
IMPLICIT NONE
CHARACTER(len=*), INTENT(IN) :: address
CHARACTER(len=18) :: defanged_address
INTEGER :: i
defanged_address = ""
DO i = 1, LEN_TRIM(address)
IF (address(i:i) == ".") THEN
defanged_address = TRIM(defanged_address) // "[.] "
ELSE
defanged_address = TRIM(defanged_address) // address(i:i)
END IF
END DO
END FUNCTION defangIPaddr
END PROGRAM defangIPaddr
This script defines a function `defangIPaddr` that takes a string `address` as input and returns a defanged version of that IP address. The function uses a loop to iterate over the characters in the `address` string and checks if each character is a period. If it is, the function appends the string "[.] " to the `defanged_address` string. Otherwise, it appends the character to the `defanged_address` string.
The script also defines a `main` program that calls the `defangIPaddr` function with two test inputs: "1.1.1.1" and "255.100.50.0". The function's output is then written to the console using the `WRITE` statement.
Note that the `LEN_TRIM` function is used to get the length of the `address` string, which is necessary because the `defanged_address` string is initialized to have a length of 18 characters.
temp.f95:8:38: 8 | defanged_address = defangIPaddr(address) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:12:38: 12 | defanged_address = defangIPaddr(address) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:17:61: 17 | FUNCTION defangIPaddr(address) RESULT(defanged_address) | 1 Error: PROGRAM attribute of βdefangipaddrβ conflicts with PROCEDURE attribute at (1) temp.f95:18:19: 18 | IMPLICIT NONE | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:19:45: 19 | CHARACTER(len=*), INTENT(IN) :: address | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:20:43: 20 | CHARACTER(len=18) :: defanged_address | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:22:18: 22 | INTEGER :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:24:27: 24 | defanged_address = "" | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:25:33: 25 | DO i = 1, LEN_TRIM(address) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:26:37: 26 | IF (address(i:i) == ".") THEN | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:27:61: 27 | defanged_address = TRIM(defanged_address) // "[.] " | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:12: 28 | ELSE | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:29:67: 29 | defanged_address = TRIM(defanged_address) // address(i:i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:11: 30 | END IF | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:31:9: 31 | END DO | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:33:9: 33 | END FUNCTION defangIPaddr | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:37:132: 37 | This script defines a function `defangIPaddr` that takes a string `address` as input and returns a defanged version of that IP address. The function uses a loop to iterate over the characters in the `address` string and checks if each character is a period. If it is, the function appends the string "[.] " to the `defanged_address` string. Otherwise, it appends the character to the `defanged_address` string. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:39:132: 39 | The script also defines a `main` program that calls the `defangIPaddr` function with two test inputs: "1.1.1.1" and "255.100.50.0". The function's output is then written to the console using the `WRITE` statement. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:41:132: 41 | Note that the `LEN_TRIM` function is used to get the length of the `address` string, which is necessary because the `defanged_address` string is initialized to have a length of 18 characters. | 1 Error: Line truncated at (1) [-Werror=line-truncation] f951: some warnings being treated as errors
def defangIPaddr(address: str) -> str:
return address.replace('.', '[.]')
The function for each language takes an input address, which is a valid IPv4 address. It iterates through each character of the string, checking for a period ('.'). If a period is found, we replace it with "[.]" in the resulting string. For other characters, we just add them to the resulting string.
In C++, Java, and JavaScript, we iterate over each character using a for loop, appending the processed character to a string object or a StringBuilder object. Java uses a StringBuilder object for more efficient string concatenation.
In Python, we can directly use the string method replace()
to replace all occurrences of the period with "[.]" in a more concise way.
At the end of the function, we return the defanged IP address as string.
#include <string>
using namespace std;
string defangIPaddr(string address) {
string defanged = "";
for (char c : address) {
if (c == '.')
defanged += "[.]";
else
defanged += c;
}
return defanged;
}
The function for each language takes an input address, which is a valid IPv4 address. It iterates through each character of the string, checking for a period ('.'). If a period is found, we replace it with "[.]" in the resulting string. For other characters, we just add them to the resulting string.
In C++, Java, and JavaScript, we iterate over each character using a for loop, appending the processed character to a string object or a StringBuilder object. Java uses a StringBuilder object for more efficient string concatenation.
In Python, we can directly use the string method replace()
to replace all occurrences of the period with "[.]" in a more concise way.
At the end of the function, we return the defanged IP address as string.