LeetCode 1108. Defanging an IP Address (LaTeX)

Given a valid (IPv4) IP address, return a defanged version of that IP address.

A defanged IP address replaces every period "." with "[.]".

Example

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"

Notes

The given address is a valid IPv4 address.

Solution

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{datetime2}
\usepackage{expl3}

\begin{document}
\setlength{\parindent}{0cm}

\ExplSyntaxOn


\cs_set:Npn \defang_ip #1 {
    \tl_set:Nn \l_tmpa_tl {#1}
    \tl_replace_all:Nnn \l_tmpa_tl {.} {[.]}
    \tl_use:N \l_tmpa_tl
}

\par\defang_ip{1.2.3.4}
\par\defang_ip{192.168.1.0}

\ExplSyntaxOff

\DTMnow

\end{document}

Output

1[.]2[.]3[.]4
192[.]168[.]1[.]0
2020-06-10 17:36:17-04:00