-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartOz.java
39 lines (34 loc) · 1.1 KB
/
StartOz.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
package Warmup01;
public class StartOz {
public static String startOz(String str) {
// Given a string, return a string made of the first 2 chars (if present),
// however include first char only if it is 'o' and
// include the second only if it is 'z',
// so "ozymandias" yields "oz".
String result = "";
if (str.length()>1){
if (str.substring(0,1).equals("o")) result = "o";
if ((!str.substring(0,1).equals("o")) && str.substring(1,2).equals("z")) result = "z";
if (str.substring(0,1).equals("o") && str.substring(1,2).equals("z")) result = "oz";
} else {
result = str;
}
return result;
}
//
// BETTER SOLUTION:
//
// public String startOz(String str) {
// String result = "";
//
// if (str.length() >= 1 && str.charAt(0)=='o') {
// result = result + str.charAt(0);
// }
//
// if (str.length() >= 2 && str.charAt(1)=='z') {
// result = result + str.charAt(1);
// }
//
// return result;
// }
}