Question: Return a version of the given string, where for every star () in the string the star and the chars immediately to its left and right are gone. So "abcd" yields "ad" and "abcd" also yields "ad". Ex. starOut("abcd") → "ad" starOut("ab cd") → "ad" starOut("smeilly") → "silly"
I know there are answers online but I don't want to copy them. I have started with most of it but do not know how to take out the spaces and convert back to a string? Also if you guys have a better solution feel free to provide that. Please don't provide Regex based solutions.
public static String starOut(String str) {
char[] array = str.toCharArray();
for(int i = 0; i < array.length; i++) {
if(array[i] == '*' && i != 0) {
if(array[i - 1] != '*') {
array[i - 1] = ' ';
}
if(array[i + 1] != '*') {
array[i + 1] = ' ';
}
}
//Rest of Code...
if()
}
return str;
}
Aucun commentaire:
Enregistrer un commentaire