Given an array of Strings, words, return that array with all Strings of an even length replaced with an empty string.
blankAllEvens( { "Hi", "there", "bob" }) => {"", "there", "bob"}
blankAllEvens({ "this", "is", "sparta!"}) => {"", "", "sparta!"}
blankAllEvens({ }) => {}
How do I return the string? I need to return the string in order to print blank space for the evens but right now I am just returning a count. This is my code I think everything is right, I just don't know how to return it?
public String[] blankAllEvens( String[] words ) {
int cnt = 0;
for (int i=0; i<words.length; i++) {
if (words[i].substring(0, 1).equals("2")) {
cnt++;
}
}
return cnt;
}
Here you go:
public String[] blankAllEvens( String[] words ) {
for (int i=0; i <words.length; i++) {
if (words[i].length() % 2 == 0) {
words[i] = "";
}
}
return words;
}
Basically, the if statement checks to see if the word in the array has an even length (we are dividing the word length by 2 and seeing if we get a remainder of 0); In that case, we blank it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.