-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontBack
More file actions
22 lines (20 loc) · 719 Bytes
/
frontBack
File metadata and controls
22 lines (20 loc) · 719 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Given a string, return a new string where the first and last chars have been exchanged.
//frontBack("code") → "eodc"
//frontBack("a") → "a"
//frontBack("ab") → "ba"
public class frontBack {
public static void main(String[] args) {
String output = frontBack("code");
System.out.println(output);
String output1=frontBack("a");
System.out.println(output1);
String output2=frontBack("ab");
System.out.println(output2);
}
public static String frontBack(String str) {
if (str.length() >= 2)
return str.substring(str.length() - 1) + str.substring(1, str.length() - 1) + str.substring(0, 1);
else
return str;
}
}