-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleThree
More file actions
23 lines (22 loc) · 756 Bytes
/
middleThree
File metadata and controls
23 lines (22 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*Given a string of odd length, return the string length 3 from its middle, so "Candy" yields "and". The string length
will be at least 3.
middleThree("Candy") → "and"
middleThree("and") → "and"
middleThree("solving") → "lvi"
*/
package string1;
public class middleThree {
public static void main(String[] args) {
String output=middleThree("Candy");
System.out.println(output);
String output1=middleThree("and");
System.out.println(output1);
String output2=middleThree("solving");
System.out.println(output2);
}
public static String middleThree(String str) {
if (str.length()%2!=0)
return str.substring(str.length()/2-1,str.length()/2+2);
return null;
}
}