Problem:
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 "ab*cd" yields
"ad" and "ab**cd" also yields "ad".
starOut("ab*cd") → "ad"
starOut("ab**cd") → "ad"
starOut("sm*eilly") → "silly"
starOut("ab*cd") → "ad"
starOut("ab**cd") → "ad"
starOut("sm*eilly") → "silly"
Solution:
package
com.nextgen4it.problems;
class
starOutfunction {
public
String starOut(String str)
{
String result
= "";
for
(int i
= 0; i < str.length();
i++) {
if
(str.charAt(i) == '*')
{
} else
if (i
!= 0 && str.charAt(i
- 1) == '*') {
} else
if (i
!= str.length() - 1 && str.charAt(i
+ 1) == '*') {
} else
{
result
+= str.charAt(i);
}
}
return
result;
}
public
static void
main(String[] args) {
starOutfunction s
= new starOutfunction();
System.out.println(s.starOut("sm*eilly"));
}
}
Output:
silly
EmoticonEmoticon