From a7bb4a5d7d8918923bcb31308b75d8616c05221f Mon Sep 17 00:00:00 2001 From: Khushali Pariyal <59405700+khushali6@users.noreply.github.com> Date: Thu, 1 Oct 2020 06:36:42 +0530 Subject: [PATCH 1/2] Add files via upload --- C++/Defanging an IP Address.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 C++/Defanging an IP Address.cpp diff --git a/C++/Defanging an IP Address.cpp b/C++/Defanging an IP Address.cpp new file mode 100644 index 00000000..f998d4eb --- /dev/null +++ b/C++/Defanging an IP Address.cpp @@ -0,0 +1,20 @@ +#include +class Solution { +public: + string defangIPaddr(string address) { + string op; + for(int i=0;address[i]!='\0';i++) + { + if(address[i]=='.') + { + + op=op+'['+ address[i]+']'; + i++; + + } + op=op+address[i]; + + } + return op; + } +}; \ No newline at end of file From c225d7124cd7e05c0e1d2a2e50632d5f88ea8702 Mon Sep 17 00:00:00 2001 From: Khushali Pariyal <59405700+khushali6@users.noreply.github.com> Date: Fri, 2 Oct 2020 08:07:07 +0530 Subject: [PATCH 2/2] Update Defanging an IP Address.cpp --- C++/Defanging an IP Address.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/C++/Defanging an IP Address.cpp b/C++/Defanging an IP Address.cpp index f998d4eb..43043ba1 100644 --- a/C++/Defanging an IP Address.cpp +++ b/C++/Defanging an IP Address.cpp @@ -1,13 +1,21 @@ +/*Given a valid (IPv4) IP address, return a defanged version of that IP address. +A defanged IP address replaces every period "." with "[.]".*/ + +/* Input: address = "1.1.1.1" + Output: "1[.]1[.]1[.]1"*/ + #include class Solution { public: string defangIPaddr(string address) { string op; +//A for loop which will execute till the address[i] which means the string runs till the last character of the string is Null for(int i=0;address[i]!='\0';i++) { + //if address[i]== '.' which means that if the in a string contains a period we'll replace it with [.] if(address[i]=='.') { - + //This line adds the square brackets if there's a period in a string op=op+'['+ address[i]+']'; i++; @@ -17,4 +25,4 @@ class Solution { } return op; } -}; \ No newline at end of file +};