forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipZap.java
More file actions
26 lines (24 loc) · 815 Bytes
/
zipZap.java
File metadata and controls
26 lines (24 loc) · 815 Bytes
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
/* Look for patterns like "zip" and "zap" in the string -- length-3, starting
* with 'z' and ending with 'p'. Return a string where for all such words,
* the middle letter is gone, so "zipXzap" yields "zpXzp".
*/
public String zipZap(String str) {
char[] arr = new char[str.length()];
int count = 0;
int i = 0;
while(i < str.length()) {
if(i < str.length() - 2 && str.charAt(i) == 'z' &&
str.charAt(i + 2) == 'p') {
arr[count] = 'z';
count++;
arr[count] = 'p';
count++;
i += 3;
} else {
arr[count] = str.charAt(i);
count++;
i++;
}
}
return new String(arr, 0, count);
}