4
4
* CSC230-02 Spring 2016
5
5
*/
6
6
7
- import java .util .Arrays ;
7
+ import java .util .Arrays ; // used in equals method
8
8
9
9
public class MyString {
10
10
@@ -21,46 +21,67 @@ public MyString() {
21
21
public MyString (String str ) {
22
22
currLength = str .length ();
23
23
strChars = new char [currLength ];
24
- ensureCapacity ();
24
+
25
25
for (int i = 0 ; i < currLength ; i ++) {
26
26
strChars [i ] = str .charAt (i );
27
27
}
28
+ ensureCapacity ();
28
29
}
29
30
30
31
// Copy Constructor
31
32
public MyString (MyString str ) {
32
33
currLength = str .currLength ;
33
34
strChars = new char [currLength ];
34
- ensureCapacity ();
35
+
35
36
for (int i = 0 ; i < currLength ; i ++) {
36
37
strChars [i ] = str .strChars [i ];
38
+
37
39
}
38
-
40
+ ensureCapacity ();
39
41
}
40
42
41
- // Length Method
43
+ // Length Method, returns length of string in array, not last array index
42
44
public int length () {
43
45
return currLength ;
44
46
}
45
47
46
48
// Allocate additional memory for the string
47
49
private void ensureCapacity () {
48
50
49
- if (this .currLength == this .strChars .length ){
50
- char [] cap = new char [this .currLength *2 ];
51
+ // increase size of array
52
+ char [] cap = new char [this .currLength + 1 ];
53
+
54
+ // pass values to temp array
55
+ for (int i = 0 ; i < currLength ; i ++){
56
+ cap [i ] = strChars [i ];
57
+ }
58
+ // new instance of strChars
59
+ strChars = new char [this .currLength + 1 ];
60
+
61
+ //pass values back
62
+ for (int a = 0 ; a < currLength ; a ++){
63
+ strChars [a ] = cap [a ];
64
+ }
65
+
51
66
}
52
67
53
- }
68
+
54
69
55
70
// return a string representation of the MyString object
56
71
public String toString () {
57
72
58
- if (strChars == null ){
73
+ if (strChars == null ){ // for empty MyString
59
74
String stringRep = new String ();
60
75
return stringRep ;
61
76
}
77
+
78
+ // copy array to trim null for printout
79
+ char [] copy = new char [currLength ];
62
80
63
- String stringRep = new String (strChars );
81
+ for (int i = 0 ; strChars [i ] != '\u0000' ; i ++){
82
+ copy [i ] = strChars [i ];
83
+ }
84
+ String stringRep = new String (copy );
64
85
return stringRep ;
65
86
}
66
87
@@ -70,19 +91,23 @@ public String toString() {
70
91
71
92
public MyString concat (MyString str ) {
72
93
int comboLength = str .currLength + currLength ;
73
- char [] copyArr = new char [comboLength ];
74
-
75
- for (int i = 0 ; i < currLength ; i ++) {
76
- copyArr [i ] = strChars [i ];
94
+
95
+ // set length of new array
96
+ char [] copyArr = new char [comboLength ];
97
+
98
+ //place first (calling) string into array
99
+ for (int i = 0 ; i < currLength ; i ++) {
100
+ copyArr [i ] = strChars [i ];
77
101
}
78
-
102
+ // place second (perameter) string into array
79
103
for (int i = currLength , a = 0 ; i < comboLength ; i ++, a ++) {
80
- copyArr [i ] = str .strChars [a ];
104
+ copyArr [i ] = str .strChars [a ];
81
105
}
82
-
83
- String stringRep = new String (copyArr );
84
-
85
- MyString Concat = new MyString (stringRep );
106
+ // turn array into string
107
+ String stringRep = new String (copyArr );
108
+
109
+ // pass string to MyString constructor
110
+ MyString Concat = new MyString (stringRep );
86
111
87
112
return Concat ;
88
113
}
@@ -93,18 +118,24 @@ public MyString concat(MyString str) {
93
118
public MyString concat (String str ) {
94
119
int strLength = str .length ();
95
120
int comboLength = strLength + currLength ;
121
+
122
+ // set length of new array
96
123
char [] copyArr = new char [comboLength ];
97
-
124
+
125
+ // place first (calling) string into array
98
126
for (int i = 0 ; i < currLength ; i ++) {
99
127
copyArr [i ] = strChars [i ];
100
128
}
101
-
129
+
130
+ //place second (peramter) string into array
102
131
for (int i = currLength , a = 0 ; i < comboLength ; i ++, a ++) {
103
132
copyArr [i ] = str .charAt (a );
104
133
}
105
-
134
+
135
+ // turn array into string
106
136
String stringRep = new String (copyArr );
107
-
137
+
138
+ //pass string to MyString constructor
108
139
MyString Concat = new MyString (stringRep );
109
140
110
141
return Concat ;
@@ -124,9 +155,12 @@ public boolean equals(MyString str) {
124
155
125
156
// or an alternative to importing java.util.Array for .equals method
126
157
public boolean equalTo (MyString str ) {
127
-
158
+
159
+ // check for length equality
128
160
if (str .currLength != currLength ) {
129
161
return false ;
162
+
163
+ // traverse arrays for inequalities
130
164
} else if (str .currLength == currLength ) {
131
165
for (int i = 0 ; i < currLength ; i ++) {
132
166
if (strChars [i ] != str .strChars [i ]) {
@@ -143,36 +177,51 @@ public boolean equalTo(MyString str) {
143
177
pos int if this MyString is alph. after perameter
144
178
*/
145
179
public int compareTo (MyString str ) {
180
+
181
+ // determine shortest string for inbounds traversal indexing
146
182
int length_diff = currLength - str .currLength ;
147
183
int charDiff = 0 ;
148
-
184
+
185
+ // calling object is shorter
149
186
if (length_diff < 0 ) {
150
187
for (int i = 0 ; i < currLength ; i ++) {
188
+
189
+ // lower case using wrapper class
151
190
charDiff = Character .toLowerCase (strChars [i ])
152
191
- Character .toLowerCase (str .strChars [i ]);
192
+
193
+ //return first difference
153
194
if (charDiff < 0 || charDiff > 0 ) {
154
195
return charDiff ;
155
196
}
156
197
}
157
198
}
158
-
199
+
200
+ // perameter is shorter or equality of lengths
159
201
if (length_diff >= 0 ) {
160
202
for (int i = 0 ; i < str .currLength ; i ++) {
203
+
204
+ // lower case using wrapper class
161
205
charDiff = Character .toLowerCase (strChars [i ])
162
206
- Character .toLowerCase (str .strChars [i ]);
207
+
208
+ // return first difference
163
209
if (charDiff < 0 || charDiff > 0 ) {
164
210
return charDiff ;
165
211
}
166
212
}
167
213
}
168
-
214
+
215
+ // return no difference
169
216
return charDiff ;
170
217
}
171
218
172
219
/* .get method, takes int, returns char at index location
173
220
int must be in range!
174
221
*/
175
222
public char get (int index ) {
223
+
224
+ // respond to out of bounds indexing
176
225
if (index < 0 || index > currLength - 1 ) {
177
226
System .out .println ("index out of bounds!" );
178
227
return '?' ;
@@ -181,12 +230,19 @@ public char get(int index) {
181
230
}
182
231
// .toUpper method, ruturns MyString that is in all upper case
183
232
public MyString toUpper () {
233
+
234
+ // copy origional to leave it unchanged
184
235
char [] strChars2 = new char [currLength ];
185
236
for (int i = 0 ; i < currLength ; i ++) {
237
+
238
+ //upper case using wrapper class
186
239
strChars2 [i ] = Character .toUpperCase (strChars [i ]);
187
240
}
241
+
242
+ // turn array into string
188
243
String stringRep = new String (strChars2 );
189
-
244
+
245
+ // pass string to MyString constructor
190
246
MyString Upper = new MyString (stringRep );
191
247
192
248
return Upper ;
@@ -196,12 +252,19 @@ public MyString toUpper() {
196
252
197
253
// .toLower method, returns MyString that is in all lower case
198
254
public MyString toLower () {
255
+
256
+ // copy origional to leave it unchanged
199
257
char [] strChars2 = new char [currLength ];
200
258
for (int i = 0 ; i < currLength ; i ++) {
259
+
260
+ // lower case using wrapper class
201
261
strChars2 [i ] = Character .toLowerCase (strChars [i ]);
202
262
}
263
+
264
+ //turn array into string
203
265
String stringRep = new String (strChars2 );
204
-
266
+
267
+ // pass string to MyString constructor
205
268
MyString Lower = new MyString (stringRep );
206
269
207
270
return Lower ;
@@ -214,29 +277,39 @@ public MyString toLower() {
214
277
If parameter is not found in the calling object, method returns -1
215
278
*/
216
279
public int indexOf (MyString str ) {
280
+
281
+ // assume no occurance initially
217
282
int index = -1 ;
218
283
284
+ // if perameter is longer than calling object, no occurance
219
285
if (str .currLength > currLength ) {
220
286
221
287
return index ;
222
288
}
223
-
289
+
290
+ // traverse calling array until perameter can no longer fits
224
291
for (int i = 0 ; i <= currLength - str .currLength ; i ++) {
225
-
292
+
293
+ // peramter of length 1
226
294
if (strChars [i ] == str .strChars [0 ] && str .currLength == 1 ) {
227
295
index = i ;
228
296
return index ;
229
297
}
230
-
298
+ // search for occurance of first letter in perameter string
231
299
if (strChars [i ] == str .strChars [0 ]) {
300
+
301
+ // check for subsequent equality
232
302
for (int a = 1 ; a < str .currLength ; a ++) {
233
303
if (strChars [i + a ] == str .strChars [a ]) {
304
+
305
+ // check for first complete occurance
234
306
if (a == str .currLength -1 ){
235
307
index = i ;
236
308
return index ;
237
309
}
238
310
}
239
311
312
+ // subsequent equality failed, back to outer for loop
240
313
else {
241
314
break ;
242
315
}
@@ -253,26 +326,37 @@ public int indexOf(MyString str) {
253
326
If parameter is not found in the calling object, method returns -1
254
327
*/
255
328
public int lastIndexOf (MyString str ) {
329
+
330
+ // assume no occurance initially
256
331
int index = -1 ;
257
332
333
+ // if perameter is longer than calling object, no occurance
258
334
if (str .currLength > currLength ) {
259
335
260
336
return index ;
261
337
}
262
-
338
+ // traverse calling array until perameter can no longer fits
263
339
for (int i = 0 ; i <= currLength - str .currLength ; i ++) {
264
-
340
+
341
+ // peramter of length 1, set index but keep looking
265
342
if (strChars [i ] == str .strChars [0 ] && str .currLength == 1 ) {
266
343
index = i ;
267
344
}
268
-
345
+
346
+ //search for occurance of first letter in perameter string
269
347
if (strChars [i ] == str .strChars [0 ]) {
348
+
349
+ // check for subsequent equality
270
350
for (int a = 1 ; a < str .currLength ; a ++) {
271
351
if (strChars [i + a ] == str .strChars [a ]) {
352
+
353
+ // for first complete occurance, set index, keep looking
272
354
if (a == str .currLength -1 ){
273
355
index = i ;
274
356
}
275
-
357
+ /*potential first occurance failed, index not set
358
+ back to outer for loop
359
+ */
276
360
} else if (index == -1 ){
277
361
break ;
278
362
}
@@ -286,12 +370,16 @@ public int lastIndexOf(MyString str) {
286
370
index.
287
371
*/
288
372
public MyString substring (int start ) {
373
+
374
+ // initialize and populate new array
289
375
char [] sub = new char [currLength - start ];
290
376
for (int n = start , a = 0 ; n < currLength ; n ++, a ++){
291
377
sub [a ] = strChars [n ];
292
378
}
379
+ // turn array into string
293
380
String stringRep = new String (sub );
294
381
382
+ //pass string to MyString constructor
295
383
MyString substring = new MyString (stringRep );
296
384
297
385
return substring ;
@@ -302,12 +390,16 @@ public MyString substring(int start) {
302
390
where n is the starting index and m is one past the ending index.
303
391
*/
304
392
public MyString substring (int start , int end ) {
393
+
394
+ // initialize and populate array
305
395
char [] sub = new char [end - start +1 ];
306
396
for (int n = start , a = 0 ; n <= end ; n ++, a ++){
307
397
sub [a ] = strChars [n ];
308
398
}
399
+ // turn array into string
309
400
String stringRep = new String (sub );
310
401
402
+ // pass string to MyString constructor
311
403
MyString substring = new MyString (stringRep );
312
404
313
405
return substring ;
0 commit comments