forked from UNCG-CSC230/program1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyString.java
408 lines (311 loc) · 12.3 KB
/
MyString.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Nathaniel Clay Arnold
* Program 1 - MyString
* CSC230-02 Spring 2016
*/
import java.util.Arrays; // used in equals method
public class MyString {
private char[] strChars;
private int currLength;
// Empty Constructor
public MyString() {
strChars = null;
currLength = 0;
}
// Constructor initialize char array to characters in String
public MyString(String str) {
currLength = str.length();
strChars = new char[currLength];
for (int i = 0; i < currLength; i++) {
strChars[i] = str.charAt(i);
}
ensureCapacity();
}
// Copy Constructor
public MyString(MyString str) {
currLength = str.currLength;
strChars = new char[currLength];
for (int i = 0; i < currLength; i++) {
strChars[i] = str.strChars[i];
}
ensureCapacity();
}
// Length Method, returns length of string in array, not last array index
public int length() {
return currLength;
}
// Allocate additional memory for the string
private void ensureCapacity() {
// increase size of array
char[] cap = new char[this.currLength + 1];
// pass values to temp array
for(int i = 0; i < currLength; i++){
cap[i] = strChars[i];
}
// new instance of strChars
strChars = new char[this.currLength + 1];
//pass values back
for(int a = 0; a < currLength; a++){
strChars[a] = cap[a];
}
}
// return a string representation of the MyString object
public String toString() {
if(strChars == null){ // for empty MyString
String stringRep = new String();
return stringRep;
}
// copy array to trim null for printout
char[] copy = new char[currLength];
for(int i = 0; strChars[i] != '\u0000'; i++){
copy[i] = strChars[i];
}
String stringRep = new String(copy);
return stringRep;
}
/* concat method takes a MyString parameter, returns MyString object
that is a concatination of of calling object and parameter
*/
public MyString concat(MyString str) {
int comboLength = str.currLength + currLength;
// set length of new array
char[] copyArr = new char[comboLength];
//place first (calling) string into array
for (int i = 0; i < currLength; i++) {
copyArr[i] = strChars[i];
}
// place second (perameter) string into array
for (int i = currLength, a = 0; i < comboLength; i++, a++) {
copyArr[i] = str.strChars[a];
}
// turn array into string
String stringRep = new String(copyArr);
// pass string to MyString constructor
MyString Concat = new MyString(stringRep);
return Concat;
}
/* overloaded concat method takes a String parameter and concatinates it
with the calling MyString object, returning a new MyString object.
*/
public MyString concat(String str) {
int strLength = str.length();
int comboLength = strLength + currLength;
// set length of new array
char[] copyArr = new char[comboLength];
// place first (calling) string into array
for (int i = 0; i < currLength; i++) {
copyArr[i] = strChars[i];
}
//place second (peramter) string into array
for (int i = currLength, a = 0; i < comboLength; i++, a++) {
copyArr[i] = str.charAt(a);
}
// turn array into string
String stringRep = new String(copyArr);
//pass string to MyString constructor
MyString Concat = new MyString(stringRep);
return Concat;
}
/* .equals method takes MyString parameter, returns true if
this MyString and the parameter are the same
*/
public boolean equals(MyString str) {
boolean equal = Arrays.equals(strChars, str.strChars);
return equal;
}
// or an alternative to importing java.util.Array for .equals method
public boolean equalTo(MyString str) {
// check for length equality
if (str.currLength != currLength) {
return false;
// traverse arrays for inequalities
} else if (str.currLength == currLength) {
for (int i = 0; i < currLength; i++) {
if (strChars[i] != str.strChars[i]) {
return false;
}
}
}
return true;
}
/* .compareTo method, takes MyString parameter, returns as follows:
0 if the parameter and this MyString are same
neg int if this MyString is alphabetically before the perameter
pos int if this MyString is alph. after perameter
*/
public int compareTo(MyString str) {
// determine shortest string for inbounds traversal indexing
int length_diff = currLength - str.currLength;
int charDiff = 0;
// calling object is shorter
if (length_diff < 0) {
for (int i = 0; i < currLength; i++) {
// lower case using wrapper class
charDiff = Character.toLowerCase(strChars[i])
- Character.toLowerCase(str.strChars[i]);
//return first difference
if (charDiff < 0 || charDiff > 0) {
return charDiff;
}
}
}
// perameter is shorter or equality of lengths
if (length_diff >= 0) {
for (int i = 0; i < str.currLength; i++) {
// lower case using wrapper class
charDiff = Character.toLowerCase(strChars[i])
- Character.toLowerCase(str.strChars[i]);
// return first difference
if (charDiff < 0 || charDiff > 0) {
return charDiff;
}
}
}
// return no difference
return charDiff;
}
/* .get method, takes int, returns char at index location
int must be in range!
*/
public char get(int index) {
// respond to out of bounds indexing
if (index < 0 || index > currLength - 1) {
System.out.println("index out of bounds!");
return '?';
}
return strChars[index];
}
// .toUpper method, ruturns MyString that is in all upper case
public MyString toUpper() {
// copy origional to leave it unchanged
char[] strChars2 = new char[currLength];
for (int i = 0; i < currLength; i++) {
//upper case using wrapper class
strChars2[i] = Character.toUpperCase(strChars[i]);
}
// turn array into string
String stringRep = new String(strChars2);
// pass string to MyString constructor
MyString Upper = new MyString(stringRep);
return Upper;
}
// .toLower method, returns MyString that is in all lower case
public MyString toLower() {
// copy origional to leave it unchanged
char[] strChars2 = new char[currLength];
for (int i = 0; i < currLength; i++) {
// lower case using wrapper class
strChars2[i] = Character.toLowerCase(strChars[i]);
}
//turn array into string
String stringRep = new String(strChars2);
// pass string to MyString constructor
MyString Lower = new MyString(stringRep);
return Lower;
}
/*.indexOf, take MyString peram. returns the starting index of the first
occurance of the MyString in the calling object
If parameter is not found in the calling object, method returns -1
*/
public int indexOf(MyString str) {
// assume no occurance initially
int index = -1;
// if perameter is longer than calling object, no occurance
if (str.currLength > currLength) {
return index;
}
// traverse calling array until perameter can no longer fits
for (int i = 0; i <= currLength - str.currLength; i++) {
// peramter of length 1
if (strChars[i] == str.strChars[0] && str.currLength == 1) {
index = i;
return index;
}
// search for occurance of first letter in perameter string
if (strChars[i] == str.strChars[0]) {
// check for subsequent equality
for (int a = 1; a < str.currLength; a++) {
if (strChars[i + a] == str.strChars[a]) {
// check for first complete occurance
if(a == str.currLength -1){
index = i;
return index;
}
}
// subsequent equality failed, back to outer for loop
else {
break;
}
}
}
}
return index;
}
/*.lastIndexOf, take my string peram. returns the starting index of the last
occurance of the MyString in the calling object
If parameter is not found in the calling object, method returns -1
*/
public int lastIndexOf(MyString str) {
// assume no occurance initially
int index = -1;
// if perameter is longer than calling object, no occurance
if (str.currLength > currLength) {
return index;
}
// traverse calling array until perameter can no longer fits
for (int i = 0; i <= currLength - str.currLength; i++) {
// peramter of length 1, set index but keep looking
if (strChars[i] == str.strChars[0] && str.currLength == 1) {
index = i;
}
//search for occurance of first letter in perameter string
if (strChars[i] == str.strChars[0]) {
// check for subsequent equality
for (int a = 1; a < str.currLength; a++) {
if (strChars[i + a] == str.strChars[a]) {
// for first complete occurance, set index, keep looking
if(a == str.currLength -1){
index = i;
}
/*potential first occurance failed, index not set
back to outer for loop
*/
} else if (index == -1){
break;
}
}
}
}
return index;
}
/*.substring(int n) returns a MyString substring, where n is the starting
index.
*/
public MyString substring(int start) {
// initialize and populate new array
char[] sub = new char[currLength - start];
for(int n = start, a = 0; n < currLength; n++, a++){
sub[a] = strChars[n];
}
// turn array into string
String stringRep = new String(sub);
//pass string to MyString constructor
MyString substring = new MyString(stringRep);
return substring;
}
/* overloaded substring(int n, int m) returns a MyString substring,
where n is the starting index and m is one past the ending index.
*/
public MyString substring(int start, int end) {
// initialize and populate array
char[] sub = new char[end - start +1];
for(int n = start, a = 0; n <= end; n++, a++){
sub[a] = strChars[n];
}
// turn array into string
String stringRep = new String(sub);
// pass string to MyString constructor
MyString substring = new MyString(stringRep);
return substring;
}
}