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:

  • The given address is a valid IPv4 address.

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
❌ Correct
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
❌ Compiled
❌ Executed
❌ Correct
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.
🌐 Data from online sources
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.

🌐 Data from online sources
#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.