-IP address invalidation algorithm
Give you a valid IPv4 address address, return this IP address is not valid version.
The so-called IP address invalidation, in fact, is to use "[.]" Instead of each. "."
Example 1:
Input: address = "1.1.1.1"
Output: "[.] [.] [.] 1 1 1 1"
Example 2:
Input: address = "255.100.50.0"
Output: "[.] [.] [.] 255100500"
prompt:
given address is a valid IPv4 address
Source: stay button (LeetCode)
class Solution {
public String defangIPaddr(String address) {
char[] old = address.toCharArray();
String newAdd = "";
for(int i = 0; i<old.length; i++){
if(old[i] != '.'){
newAdd = newAdd + old[i];
}
else{
newAdd = newAdd + "[.]";
}
}
return newAdd;
}
}
Note
one of a character string is replaced with a method of another character:
String old = "Hello";
String newWord;
newWord = old.replace('l','w');
The above code output is "Hewwo"