-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBuilderExercises
More file actions
94 lines (82 loc) · 2.84 KB
/
StringBuilderExercises
File metadata and controls
94 lines (82 loc) · 2.84 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
public class ArrayListExercises {
public static void main(String[] args) {
// Problem One
StringBuilder text = new StringBuilder("ABCDEFG");
System.out.println(text);
// Deletes DE
text.delete(3, 5);
System.out.println(text);
text.insert(3, "DE");
System.out.println(text);
// at E, puts in XYZ
text.insert(4, "XYZ");
System.out.println(text);
text.delete(4, 8);
text.insert(4, "E");
System.out.println(text);
// replaces C to F with XYZ
text.replace(2, 7, "XYZ");
System.out.println(text);
text.replace(2, 5, "CDEFG");
System.out.println(text);
// Changes B to X
text.setCharAt(1, 'X');
// Deletes C
text.deleteCharAt(2);
// Sets F to Y
text.setCharAt(4, 'Y');
// Flips the Text
text.reverse();
System.out.println(text);
text.reverse();
text.setCharAt(4, 'F');
text.insert(2, 'C');
text.setCharAt(1, 'B');
System.out.println(text);
// Problem Two
StringBuilder strBuild = new StringBuilder(10);
// Capacity has been set at 10, but the length is 0, all null
System.out.println(strBuild.length() + " " + strBuild.capacity());
// Problem Three
String sentence = "He wore a blue shirt and black pants.";
System.out.println(sentence);
String newStr = searchAndReplace(sentence, "blue", "black");
System.out.println(newStr);
// Problem Four
Scanner keyboard = new Scanner(System.in);
String comparisonLine = "abcdefghijklmnopqrstuvwxyz";
String conversionLine = "22233344455566677778889999";
StringBuilder convertedWord = new StringBuilder();
System.out.println("What is the word you want to convert?");
StringBuilder wordConversion = new StringBuilder(keyboard.next().toLowerCase());
for (int index = 0; index < wordConversion.length(); index++) {
// Gets integer value of the character
convertedWord.append(conversionLine.charAt((comparisonLine.indexOf(wordConversion.charAt(index)))));
}
System.out.println(convertedWord);
// Problem Five
System.out.println("What is the Word you want to encode?");
String word = (keyboard.next().toUpperCase());
String encodedWord = encodeWord(word);
System.out.println(encodedWord);
}
// Method for Problem Three
private static String searchAndReplace(String sentence, String search, String replace) {
StringBuilder newSentence = new StringBuilder(sentence);
while (newSentence.indexOf(search) != -1) {
newSentence.replace(newSentence.indexOf(search), replace.length() + newSentence.indexOf(search) - 1,
replace);
}
return newSentence.toString();
}
// Method for Problem Five
private static String encodeWord(String word) {
StringBuilder encodedWord = new StringBuilder();
String codeLine = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int index = 0; index < word.length(); index++) {
encodedWord.append(codeLine.indexOf(word.charAt(index)) + "-");
}
return encodedWord.toString();
}
}