Given an IP address, remove leading zeros from the IP address.
Examples:
Input : 100.020.003.400 Output : 100.20.3.400 Input :001.200.001.004 Output : 1.200.1.4
The approach is to split the given string by “.” and then convert it to an integer which removes the leading zeros and then join back them to a string.To convert a string to an integer we can use int(s) and then convert it back to string by str(s) and then join them back by using join function.
Implementation:
C++
// Cpp program to remove leading zeros // an IP address and print the IP #include <bits/stdc++.h> using namespace std; // function to remove leading zeros string removeZeros(string s) { vector<string> v; // splits the ip by "." for ( int i = 0; i < s.length(); i++) { string ans; while (i < s.length() && s[i] != '.' ) { ans += s[i]; i++; } v.push_back(ans); } vector< int > num; // converts the words to integers to remove leading removeZeros for ( auto str : v) { int temp = 0; for ( int i = 0; i < str.length(); i++) { temp *= 10; temp += (str[i] - '0' ); } num.push_back(temp); } string ans = "" ; // Convert back the integer to string and join them back to a string for ( auto i : num) { ans += '.' ; string temp; while (i) { temp += ( '0' + (i % 10)); i /= 10; } reverse(temp.begin(), temp.end()); ans += temp; } return ans.substr(1); } int main() { string ip; // example1 ip = "100.020.003.400" ; cout << (removeZeros(ip)) << "\n" ; // example2 ip = "001.200.001.004" ; cout << (removeZeros(ip)) << "\n" ; return 0; } |
Python
# Python program to remove leading zeros # an IP address and print the IP # function to remove leading zeros def removeZeros(ip): # splits the ip by "." # converts the words to integers to remove leading removeZeros # convert back the integer to string and join them back to a string new_ip = "." .join([ str ( int (i)) for i in ip.split( "." )]) return new_ip ; # driver code # example1 ip = "100.020.003.400" print (removeZeros(ip)) # example2 ip = "001.200.001.004" print (removeZeros(ip)) |
100.20.3.400 1.200.1.4
Method 2 : Regex
Using a capture group, match the last digit and copy it and prevents all the digits from being replaced.
regex \d can be explained as:
- \d : Matches any decimal digit
\d Matches any decimal digit, this is equivalent to the set class [0-9].
- \b allows you to perform a “whole words only” search using a regular expression in the form of \bword\b
regex \b can be explained as:
\b allows you to perform a "whole words only" search u sing a regular expression in the form of \bword\b
Implementation:
Python
# Python program to remove leading zeros # an IP address and print the IP using regex import re # function to remove leading zeros def removeZeros(ip): new_ip = re.sub(r '\b0+(\d)' , r '\1' , ip) # splits the ip by "." # converts the words to integers to remove leading removeZeros # convert back the integer to string and join them back to a string return new_ip # driver code # example1 ip = "100.020.003.400" print (removeZeros(ip)) # example2 ip = "001.200.001.004" print (removeZeros(ip)) |
100.20.3.400 1.200.1.4
Method 3: Storing the index of the first non zero character.
The approach is to split the string by “.” and store it in another string x.
Now get the index of the first non-zero character of that string and store
it in another variable ind. Initialise that variable to -1 which indicates that there is
no non-zero character in the string .In another variable append the substring picking
up characters from that index till the end in another string y.
Source code : c++ solution
C++
#include<bits/stdc++.h> using namespace std; int main() { string s= "152.02.0015.00" ; string newIPAdd(string); cout<<newIPAdd(s); return 0; } string newIPAdd (string s) { long long int i,l,f=0,ind=-1; // initialising ind to -1 to indicate that there is no non-zero character in the string string x= "" ,y= "" ; char c; s=s+ "." ; l=s.length(); for (i=0;i<l;i++) { c=s.at(i); if (c!= '.' ) { x=x+c; if (c!= '0' && f==0) { ind=x.length()-1; // getting the index of first non-zero character f=1; //flag=1 to prevent further change of index for another non-zero character } } else { if (ind==-1) y=y+ "." + "0" ; else y=y+ "." +x.substr(ind); //appending it to the another string ind=-1;f=0;x= "" ; } } return y.substr(1); } |
152.2.15.0