forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetSandwich.java
More file actions
28 lines (24 loc) · 926 Bytes
/
getSandwich.java
File metadata and controls
28 lines (24 loc) · 926 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
27
28
/* A sandwich is two pieces of bread with something in between. Return the
* string that is between the first and last appearance of "bread" in the
* given string, or return the empty string "" if there are not two pieces
* of bread.
*/
public String getSandwich(String str) {
int first = -1;
int last = -1;
for(int i = 0; i < str.length() - 5; i++) {
if(str.substring(i, i + 5).equals("bread")) {
first = i;
break;
}
}
for(int i = str.length() - 5; i >= 0; i--) {
if(str.substring(i, i + 5).equals("bread")) {
last = i;
break;
}
}
if(first != -1 && last != -1 && first != last)
return str.substring(first + 5, last);
return "";
}