Defanged Version of Internet Protocol Address


Introduction

This tutorial deals with the problem of finding the defanged version of the Internet Protocol Address. The Internet Protocol Address or IP Address is an individual numerical address of a device connected to or using the Internet. It is unique for every internet-connected device. It consists of numbers separated by the period (dot). An example would be 191.0.1.2.

Defanged Version of Internet Protocol means replacing the periods (dot.) with other characters so that it is not treated as a valid IP address. For defanging the Internet Protocol Address we use [.] instead of periods ( dot .) The Internet Protocol Address is defanged so that it can be securely used in text form or some code without violating coding rules.

In this tutorial, implement two approaches to defang the IP address by taking a valid IP address and replacing the dot(.) with [].

  • Replacing dot(.) with [] by traversing string.

  • Using regular expression − Regular expression or regex is a string-matching tool that is used for matching a string with some sequence of characters.

Demonstration 1

Input = String = "192.168.1.1"
Output = 192[.]168[.]1[.]1

Explanation

The valid Internet Protocol address in the form of a string is 192.168.1.1. The separators/ periods ( dot.) are replaced with [.]. The defanged IP address is 192[.]168[.]1[.]1

Demonstration 2

Input = String = "191.0.1.2"
Output = 191[.]0[.]1[.]2

Explanation

The valid Internet Protocol address in the form of a string is 191.0.1.2 The periods (dot .) are replaced with [.]. The defanged IP address is 191[.]0[.]1[.]2

C++ Library Function

regex_replace() − It is a predefined function of a regular expression. It is defined in the <regex> header file. It replaces the matched pattern with a replacement expression. It takes 3 parameters: the string to be converted, the regular pattern, and the replacement string.

Syntax

regex_replace(str, pattern, replacement);

Algorithm

  • Initialize a string with a valid IP address.

  • Use the "for" loop to traverse the characters of the string.

  • Use the if-else condition to replace (.) with [.]

  • Repeat step 3 till the loop reaches the end of the string.

  • Print the defanged IP address.

Example 1

Implement the problem statement in this tutorial in C++ by traversing the string. The string is a valid Internet Protocol address. Used loops to traverse the string and the if-else condition to replace the string separator with [.]

#include <iostream>
#include <string>
using namespace std;

//User-defined function to find the defanged IP address
string defangIPaddress(const string& str) {
   string defangAddress;

// loop to iterate the string 
   for (char ch : str){
   
      //condition to identify the . in the string
      if (ch == '.') {
         defangAddress += "[.]";
      } else {
         defangAddress += ch;
      }
   }
   //returning the defanged string
   return defangAddress;
}

//code controller
int main() {
   string validIP = "192.168.1.1";
   
   //calling function 
   string defanged_address = defangIPaddress(validIP);
   cout << "Original IP Address: " << validIP << endl;
   cout << "Defanged IP Address: " << defanged_address << endl;
   return 0;
}

Output

Original IP Address: 192.168.1.1
Defanged IP Address: 192[.]168[.]1[.]1

Example 2

Implemented the problem statement of this tutorial in C++ with the powerful string-matching concept of regular expressions. It is used for matching some patterns in a string. It is easy to use.

#include <iostream>
#include <regex>
#include <string>
using namespace std;

//Function for defanging the Internet Protocol Address
string defangIPaddress(const string& str) {

   // Define the regular expression stringPattern to match periods (dots)
   regex stringPattern("\.");
   
   // Define the replace string as "[.]"
   string replace = "[.]";
   
   // Use regex_replace to replace the matched periods with "[.]"
   string defangedAddress = regex_replace(str, stringPattern, replace);
   return defangedAddress;
}

//code controller
int main(){

   //Valid IP address
   string validAddress = "192.168.1.1";
   
   //calling function
   string defanged_address = defangIPaddress(validAddress);
   cout << "Original IP Address: " << validAddress << endl;
   cout << "Defanged IP Address: " << defanged_address << endl;
   return 0;
}

Output

Original IP Address: 192.168.1.1
Defanged IP Address: 192[.]168[.]1[.]1

Conclusion

We have reached the end of this tutorial. In this tutorial, we developed methods to find a defanged IP address from a valid IP address. To defang an IP address replace the periods with [.]. For replacing we used a simple string traversing method and a regular expression approach.

Regular expressions are string-matching tools for finding whether a pattern exists in a string or not. It has various predefined functions. Demonstrated the problem statement with some examples.

Updated on: 03-Oct-2023

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements