-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeOutWord.java
More file actions
51 lines (48 loc) · 1.53 KB
/
Copy pathMakeOutWord.java
File metadata and controls
51 lines (48 loc) · 1.53 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Given an "out" string length 4, such as "<<>>", and a word,
return a new string where the word is in the middle of the
out string, e.g. "<<word>>". Note: use str.substring(i, j)
to extract the String starting at index i and going up to
but not including index j.
*/
package codingbat;
import java.util.Scanner;
public class MakeOutWord {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char bracket;
String skeleton, word;
System.out.print("Enter a bracket: ");
bracket = sc.next().charAt(0);
System.out.print("Enter a word: ");
word = sc.next();
skeleton = createSkeleton(bracket);
insert(skeleton, word);
}
public static String createSkeleton(char bracket){
String skel;
if(bracket == '(' || bracket == ')'){
return skel = "(())";
}
if(bracket == '{' || bracket == '}'){
return skel = "{{}}";
}
if(bracket == '[' || bracket == ']'){
return skel = "[[]]";
}
if(bracket == '<' || bracket == '>'){
return skel = "<<>>";
}
return "";
}
public static void insert(String skeleton, String word){
String result="";
for(int i = 0; i < 4; i++){
if(i == 2){
result = result + word;
}
result = result + skeleton.charAt(i);
}
System.out.println("Result: "+result);
}
}