Given a valid (IPv4) Internet Protocol address S, the task is to find the defanged version of that IP address.
Defanged Version of IP Address: is in which every period “.” is replaced by “[.]”.
Examples:
Input: S = “1.1.1.1”
Output: 1[.]1[.]1[.]1
Input: S = “255.100.50.0”
Output: 255[.]100[.]50[.]0
Approach: The idea is to traverse the string and append every character of the string into the final answer string except when the current character is “.”, then append “[.]” into the final answer string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string GeberateDefangIP(string str)
{
string defangIP = "" ;
for ( char c : str)
(c == '.' ) ? defangIP += "[.]" :
defangIP += c;
return defangIP;
}
int main()
{
string str = "255.100.50.0" ;
cout << GeberateDefangIP(str);
return 0;
}
|
Java
class GFG{
static String GeberateDefangIP(String str)
{
String defangIP = "" ;
for ( int i = 0 ; i < str.length(); i++)
{
char c = str.charAt(i);
if (c == '.' )
{
defangIP += "[.]" ;
}
else
{
defangIP += c;
}
}
return defangIP;
}
public static void main(String[] args)
{
String str = "255.100.50.0" ;
System.out.println(GeberateDefangIP(str));
}
}
|
Python3
def GeberateDefangIP( str ):
defangIP = "";
for c in str :
if (c = = '.' ):
defangIP + = "[.]"
else :
defangIP + = c;
return defangIP;
str = "255.100.50.0" ;
print (GeberateDefangIP( str ));
|
C#
using System;
class GFG{
static String GeberateDefangIP( string str)
{
string defangIP = "" ;
for ( int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c == '.' )
{
defangIP += "[.]" ;
}
else
{
defangIP += c;
}
}
return defangIP;
}
public static void Main()
{
string str = "255.100.50.0" ;
Console.Write(GeberateDefangIP(str));
}
}
|
Javascript
<script>
function GeberateDefangIP(str)
{
var defangIP = "" ;
str.split( '' ).forEach( function (letter) {
(letter == '.' ) ? defangIP += "[.]" :
defangIP += letter; })
return defangIP;
}
var str = "255.100.50.0" ;
document.write( GeberateDefangIP(str));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)
We can improve the above solution by using Regular Expressions
C++
#include <iostream>
#include <regex>
using namespace std;
void Defanged_IP(string Str) {
regex pattern( "\\." );
string x = regex_replace(Str, pattern, "[.]" );
cout << x << endl;
}
int main() {
string Str = "1.1.1.2" ;
Defanged_IP(Str);
string S = "255.100.50.0" ;
Defanged_IP(S);
return 0;
}
|
Python3
import re
def Defanged_IP( Str ):
x = re.sub( "[.]" , "[.]" , Str )
print (x)
Str = "1.1.1.2"
Defanged_IP( Str )
S = "255.100.50.0"
Defanged_IP(S)
|
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
static void Defanged_IP(String Str) {
Pattern pattern = Pattern.compile( "\\." );
Matcher matcher = pattern.matcher(Str);
String x = matcher.replaceAll( "[.]" );
System.out.println(x);
}
public static void main(String[] args) {
String Str = "1.1.1.2" ;
Defanged_IP(Str);
String S = "255.100.50.0" ;
Defanged_IP(S);
}
}
|
Javascript
function defangedIP(str) {
const pattern = /\./g;
const x = str.replace(pattern, "[.]" );
console.log(x);
}
const str1 = "1.1.1.2" ;
defangedIP(str1);
const str2 = "255.100.50.0" ;
defangedIP(str2);
|
C#
using System;
using System.Text.RegularExpressions;
public class Program {
static void Defanged_IP(String Str) {
Regex pattern = new Regex( "\\." );
String x = pattern.Replace(Str, "[.]" );
Console.WriteLine(x);
}
public static void Main() {
String Str = "1.1.1.2" ;
Defanged_IP(Str);
String S = "255.100.50.0" ;
Defanged_IP(S);
}
}
|
Output1[.]1[.]1[.]2
255[.]100[.]50[.]0
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)
Please Login to comment...