-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStringManipulation.java
More file actions
154 lines (119 loc) · 4.58 KB
/
Copy pathStringManipulation.java
File metadata and controls
154 lines (119 loc) · 4.58 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.util.ArrayList;
public class StringManipulation implements StringManipulationInterface {
private String string;
private int count;
@Override
public String getString() {
return string;
}
@Override
public void setString(String string) {
this.string = string;
}
@Override
public int count() {
// initialize word count to 0
count = 0;
// if statement to check if string is null or empty
if (string == null || string.length() == 0) {
return count;
}
// split the string into a string array, delimiting by one or more spaces
String[] wordArray = this.string.split("\\s+");
count = wordArray.length;
return count;
}
@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
if (n > string.length()) {
throw new IndexOutOfBoundsException();
} else if (n <= 0) {
throw new IllegalArgumentException();
}
// initialize an ArrayList of Characters
ArrayList<Character> charArray = new ArrayList<Character>();
// iterate through string and insert each Character in the ArrayList
for (int i = 0; i < string.length();i ++) {
charArray.add(string.charAt(i));
}
// determine the highest Nth index less than or equal to the length of this.string
int limit = string.length() / n;
// initialize an int array to hold all the indexes to replace
int[] indexesToReplace = new int[limit];
// assign each Nth index to an element in the int array
for (int i = 1, j = 0; i <= limit; i++, j++) {
indexesToReplace[j] = n * i;
}
//System.out.println("indexes to replace: " + Arrays.toString(indexesToReplace));
// iterate through the ArrayList and replace the Character with space or delete
for (int i = indexesToReplace.length - 1; i >= 0; i--) {
int index = indexesToReplace[i] - 1;
if (maintainSpacing == true) {
charArray.set(index, ' ');
}
if (maintainSpacing == false) {
charArray.remove(index);
}
}
// build the string from the ArrayList and return to main
StringBuilder stringBuilder = new StringBuilder();
for (char c : charArray) {
stringBuilder.append(c);
}
return stringBuilder.toString();
}
@Override
public String[] getSubStrings(int startWord, int endWord){
// exception: If either "startWord" or "endWord" are invalid
if (startWord <= 0 || endWord <= 0 || startWord > endWord) {
throw new IllegalArgumentException();
}
// exception: If the string has less than "endWord" words in it
if (count() < endWord) {
throw new IndexOutOfBoundsException();
}
// split the words into a String array, delimited by one or more spaces
String[] arr = this.string.split("\\s+");
// intialize the return array with size of the number of words to keep
String[] finalArray = new String[endWord - startWord + 1];
// iterate through the array of all words, if the index falls inside the
// range passed in, then put that word in the new array
for (int i = 0, j = 0; i <= arr.length; i++) {
if (i >= startWord && i <= endWord) {
finalArray[j] = arr[i - 1];
j++;
}
}
return finalArray;
}
@Override
public String restoreString(int[] indices){
// exception: if not s.length == indices.length == n
if (string.length() != indices.length) {
throw new IllegalArgumentException();
}
// exception: if indices[i] < 0 or indices[i] > string length
for (int i = 0 ; i < indices.length; i++) {
if (indices[i] < 0 || indices[i] >= string.length()) {
throw new IndexOutOfBoundsException();
}
}
// initialize an ArrayList for the final string to be returned
ArrayList<Character> finalWord = new ArrayList<>();
// fill the ArrayList with characters from the original string
for (char c : string.toCharArray()) {
finalWord.add(c);
}
// iterate through both the indices parameter and the string characters
// to load the final array with shuffled characters
for (int i = 0; i < string.length(); i++) {
finalWord.set(indices[i], string.charAt(i));
}
// build the string from the ArrayList and return to main
StringBuilder stringBuilder = new StringBuilder();
for (char c : finalWord) {
stringBuilder.append(c);
}
return stringBuilder.toString();
}
}