forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarOut.java
40 lines (33 loc) · 1.38 KB
/
starOut.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* 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".
*/
public String starOut(String str) {
if(str.length() < 1)
return "";
if(str.length() == 1) {
if(str.charAt(0) == '*')
return "";
else
return str;
}
char[] arr = new char[str.length()];
int count = 0;
if(str.charAt(0) != '*' && str.charAt(1) != '*') {
arr[count] = str.charAt(0);
count++;
}
for(int i = 1; i < str.length() - 1; i++) {
if(str.charAt(i-1)!='*' && str.charAt(i) != '*' &&
str.charAt(i+1) != '*') {
arr[count] = str.charAt(i);
count++;
}
}
if(str.charAt(str.length()-1) != '*' &&
str.charAt(str.length()-2) != '*') {
arr[count] = str.charAt(str.length() - 1);
count++;
}
return new String(arr, 0, count);
}