-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsstring.cpp
More file actions
709 lines (583 loc) · 17.7 KB
/
Copy pathdsstring.cpp
File metadata and controls
709 lines (583 loc) · 17.7 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
* Assignment: Final Project
* Description: This project will assess static code
* Date: 12-6-2016
* Name of Programmer: Jeremy Brachle
* Student Number: 46659942
* Course Number: 2341-802
* Lab Section: N11
*/
//DSString.cpp
#include "DSString.h"
#include <cstring>
//-------------------------- BEGIN CONSTRUCTOR / DESTRUCTOR SECTION -------------------------------------
//default constructor
String::String() : data(nullptr), len(0), cap(0){}
//copy constructor string
String::String(const String &rhs)
{
if (rhs.data != nullptr) //if right is NOT null
{
data = new char[(rhs.len + 1)]; //add 1 to account for null terminator
len = rhs.len; //do NOT add 1 since you do not count the null-terminator
cap = len + 2; //make capacity 2 more than the length to have an empty slot after the null terminator
for (int i = 0; i < len; i++)
{
data[i] = rhs.data[i]; //propogate values from the right hand side into the left
}
data[len] = '\0'; // null terminator
}
else //if right IS null
{
data = nullptr;
cap = 0;
len = 0;
}
}
//copy constructor char*
String::String(const char* rhs)
{
if (rhs != nullptr) //if right is NOT null
{
data = new char[strlen(rhs) + 1]; // add 1 to account for the null terminator
len = strlen(rhs); //do NOT add 1, since you do not have to account for a null terminator
cap = len + 2; //make capacity 2 more than the length to have an empty slot after the null terminator
for (int i = 0; i < len; i ++)
{
data[i] = rhs[i]; //propogate values from right into left
}
data[len] = '\0'; //null-terminator
}
else //if right IS null
{
data = nullptr;
cap = 0;
len = 0;
}
}
//destructor
String::~String()
{
if(data != nullptr)//make sure the object is not already null
{
delete[] data;
}
}
//-------------------------- END CONSTRUCTOR / DESTRUCTOR SECTION -------------------------------------
//------------------------------ BEGIN ASSIGNMENT OPERATOR SECTION -------------------------------------
//overload assignment operator String
String& String::operator= (const String &rhs)
{
if (this != &rhs) //check for self-assignment
{
if (rhs.data != nullptr) //if right is NOT null
{
if (data != nullptr) //if left is NOT null
{
//copy right into the left
delete[] data;
cap = rhs.cap;
len = rhs.len;
data = new char[rhs.len + 1];
for (int i = 0; i < len; i++)
{
data[i] = rhs.data[i];
}
data[len] = '\0'; //null-terminator
}
else //if left IS null
{
//create new object
data = new char[rhs.len + 1]; // add 1 to account for the null terminator
len = rhs.len; //do NOT add 1 (because you do not account for a null terminator)
cap = len + 2; //make capacity 2 more than the length to have an empty slot after the null terminator
for (int i = 0; i < len; i ++)
{
data[i] = rhs.data[i]; //propogate right into left
}
data[len] = '\0'; //null-terminator
}
}
else //if right IS null
{
//since right is null, make the left null too
//check to see if left is NULL
//if left is not null
if (data != nullptr)
{
delete[] data;
data = nullptr;
cap = 0;
len = 0;
}
//if left is null
else
{
data = nullptr;
cap = 0;
len = 0;
}
}
}
else // if self assignment
{
return *this;
}
//return this if all else fails
return *this;
}
//overload assignment operator const *
String& String::operator= (const char* rhs)
{
if (rhs != nullptr) //if right is NOT null
{
if (data != nullptr)
{
delete[] data;
}
data = new char[strlen(rhs) + 1]; // add 1 to account for the null terminator
len = strlen(rhs); //do NOT add 1 (because you do not account for a null terminator)
cap = len + 2; //make capacity 2 more than the length to have an empty slot after the null terminator
for (int i = 0; i < len; i ++)
{
data[i] = rhs[i];
}
data[len] = '\0'; //null-terminator
}
else //if right IS null
{
data = nullptr;
cap = 0;
len = 0;
}
//return this if all else fails
return *this;
}
//------------------------------ END ASSIGNMENT OPERATOR SECTION ----------------------------------------
//------------------------------ BEGIN BOOLEAN OPERATOR SECTION -------------------------------------
//equality operator for when
//lhs is a String and rhs is a char*
bool String::operator==(const char *rhs)
{
if (this->size() != strlen(rhs)) //compare the size
{
return false; //automatically return false if the sizes are not even the same
}
//check to see if the data inside is equal
for (int i = 0; i < this->size(); i ++)
{
if (this->data[i] != rhs[i])
{
return false; //return false as soon as a character is not the same
}
}
return true; //return true if both the size is the same and al the characters inside both are the same
}
//equality NOT operator for when
//lhs is a String and rhs is a char*
bool String::operator !=(const char* rhs)
{
String left = this->data;
String right = rhs;
//return the opposite of ==
//if the same
if (left == right)
{
//return the opposite
return false;
}
//if not the same
else
{
//return true
return true;
}
}
//equality NOT operator for when
//lhs is a String and rhs is a string
bool String::operator !=(const String& rhs)
{
String left = this->data;
String right = rhs;
//return the opposite of ==
//if the same
if (left == right)
{
//return the opposite
return false;
}
//if not the same
else
{
//return true
return true;
}
}
//equality operator for when
//lhs is a String and rhs is a String
bool operator == (const String &lhs, const String &rhs)
{
if (lhs.len != rhs.len)
{
return false;
}
//check data in strings
for (int i = 0; i < lhs.len; i ++)
{
if (lhs.data[i] != rhs.data[i])
{
return false;
}
}
return true;
}
//greater than
bool operator > (const String &lhs, const String &rhs)
{
//for iterating index
int index = 0;
//make copies of left and right for boolean comparison
String left = lhs.data;
String right = rhs.data;
//make sure strings are NOT the same
if (left!=right)
{
//while values of both strinngs are the same
while (lhs.data[index] == rhs.data[index])
{
//increment index
index++;
}
//check for the first occurence when the characters are NOT the same
if (lhs.data[index] != rhs.data[index])
{
//if left is bigger
if (lhs.data[index] > rhs.data[index])
{
return true;
}
//if right is bigger
else if (rhs.data[index] > lhs.data[index])
{
return false;
}
}
}
//if they are the same
else
{
return false;
}
//return false if all else fails
return false;
}
//less than
bool operator < (const String & lhs, const String & rhs)
{
//for iterating index
int index = 0;
//make copies of left and right for boolean comparison
String left = lhs.data;
String right = rhs.data;
//make sure strings are NOT the same
if (left!=right)
{
//while characters of both strinngs are the same
while (lhs.data[index] == rhs.data[index])
{
//increment index
index++;
}
//check for the first occurence when the characters are NOT the same
if (lhs.data[index] != rhs.data[index])
{
//if left is smaller
if (lhs.data[index] < rhs.data[index])
{
return true;
}
//if right is smaller
else if (rhs.data[index] < lhs.data[index])
{
return false;
}
}
}
//if they ARE the same
else
return false;
//return false if all else fails
return false;
}
//------------------------------ END BOOLEAN OPERATOR SECTION -------------------------------------
//---------------------------- BEGIN MISCELLANEOUS FUNCTION SECTION -------------------------------------
//output to console
std::ostream& operator <<(std::ostream& out, const String& line)
{
out<<line.data;
return out; //enables cascading
}
//return the size/length
int String::size()
{
return this->len; //returns the variable for length
}
//return the capcity (2 more than length)
int String::capacity()
{
return this->cap;
}
//return the String this but in all uppercase
String String::toUpper()
{
String temp = *this; //create a modifiable copy of the string
for (int i = 0; i < this->size(); i++) //iterate through the string
{
if (temp.data[i] >= 97 && temp.data[i] <= 122) //check to see if lower case
{
temp.data[i] -= 32; //subtract ascii value to get to uppercase
}
}
return temp; //return string in all uppercase
}
//return the String this but in all lowercase
String String::toLower()
{
String temp = *this; //create a modifiable copy of the string
for (int i = 0; i < this->size(); i ++) //iterator through the string
{
if (temp.data[i] >= 65 && temp.data[i] <= 90) //check to see if upper case
{
temp.data[i] += 32; //add ascii value to get lowercase
}
}
return temp; //return string in all lowercase
}
//subscript operator with wrapper
char& String::operator[] (const int sub)
{
if (sub == 0) //if the index IS zero
{
return data[sub];
}
else if (sub >= 0 && sub < this->size()) //positive and within bounds
{
return data[sub];
}
else if (sub >= 0 && sub >= this->size()) //positive but out of bounds
{
return data[(sub % (this->size()))];
}
else if (sub < 0 && (sub * -1) < this->size()) //negative and within bounds
{
return data[(sub + (this->size()))];
}
else if (sub < 0 && (sub * -1) >= this->size()) //negative and out of bounds
{
if ((sub % (this->size())) == 0) //if it wraps around to just be the starting position
{
return data[(sub % (this->size()))];
}
else //if it wraps around to be anything other than starting
{
return data[(sub % (this->size())) + (this->size())];
}
}
else{
return *this->c_str();
}
}
//string concatenation
String String::operator+(const String& rhs)
{
//make a temp string
String temp;
int tempIndex = 0;
//update the length, capacity, and data
temp.len = this->len + rhs.len;
temp.cap = temp.len + 2; //one more than null-terminator
temp.data = new char[temp.len + 1];
for (int i = 0; i < this->len; i ++)
{
temp.data[i] = this->data[i];
}
for (int j = this->len; j < temp.len; j ++)
{
temp.data[j] = rhs.data[tempIndex];
tempIndex += 1;
}
temp.data[temp.len] = '\0'; //null terminator
return temp; //enables cascading
}
//string + int concatenation
String String::operator+(const int& rhs)
{
//make a temp string
String finalString;
//int finalIndex = 0;
//make a string stream object
std::stringstream number;
//read into the string stream object
number<<rhs;
//make a string for just the number
String temp = number.str().c_str();
//call the string + string function
finalString = *this + temp;
//enable cascading
return finalString;
}
//string + double concatenation
String String::operator+(const double& rhs)
{
//make a temp string
String finalString;
//int finalIndex = 0;
//make a string stream object
std::stringstream number;
//read into the string stream object
number<<rhs;
//make a string for just the number
String temp = number.str().c_str();
//call the string + string function
finalString = *this + temp;
//enable cascading
return finalString;
}
//string += string operator
String String::operator +=(const String& rhs)
{
//update this to be the addition of lhs and rhs
*this = *this + rhs;
//return this
return *this;
}
//string += char operator
String String::operator +=(const char* rhs)
{
//make string for the char argument
String temp = rhs;
//update this to be the addition of lhs and rhs
*this = *this + temp;
//return this
return *this;
}
//c_string
char* String::c_str()
{
/*
//create a new char*
char* temp = new char[this->size() + 1];
for (int i = 0; i < (this->size()); i++)
{
temp[i] = this->data[i]; //propogate the new char* with the characters from the given String
}
temp[this->size()] = '\0'; // null terminator
return temp; //return the new char*
*/
return this->data;
}
//substring given start and stop
String String::substring(int start, int end)
{
/*
For my substring functionality, I will not be using negative bounds and I will not wrap around if
bounds that exceed the length are given. Also, the lower bounds cannot be greater than or equal to
the upper bounds. If any of these are passed into the substring function as arguments, I will
simply return a blank string.
*/
String temp; //create a blank temporary string for creating the new substring
int length = this->size(); //create a variable for the given string's size
int counter = 0; //create a counter for finding the substring's size
//now check to see what starting and ending positions were given:
if (start < 0 || end < 0) //if negative bounds are given
{
temp = "";
return temp; //return empty string
}
else if (start == end) // if the upper bound is the same as the lower bound
{
temp = "";
return temp; //return empty string
}
else if (start >= length || end > length) //if the bounds exceed the length of the given string
{
temp = "";
return temp; //return empty string
}
else if (start > end) // if the lower bounds is greater than the upper bounds
{
temp = "";
return temp; //return empty string
}
else if (start < end) //if the lower bounds is less than the upper bounds
{
//add one to account for null-terminator
temp.data = new char[(end-start) + 1];
//set length to not account for null-terminator
temp.len = (end - start);
//set capacity to be 2 more than the length
temp.cap = len + 2;
//loop through and copy data
for (int i = start; i < end; i ++)
{
//set each character
temp.data[counter] = this->data[i];
//temp[counter] = this->data[i];
counter++;
}
temp.data[counter] = '\0';
//temp.len = counter;
//temp.cap = temp.len + 2;
return temp; //return the substring
}
//return fthis if all else fails
return *this;
}
//reverse the contents of a string
String String::reverse()
{
String temp = ""; //empty string that will hold the given string in reverse order
int index = (this->size() - 1); // create a variable that will hold the upper bound index
for (int i = 0 ; i < this->size(); i ++)
{
temp.data[i] = this->data[index]; //assign the rightmost index to the start of the new string
index--; //decrement the index
}
temp.data[this->size()] = '\0'; //assign null-terminator to the end of the new string
temp.len = this->size(); //set the length equal to the original string
temp.cap = temp.len + 2; //set the capacity to 2 more than the length
return temp;
}
//remove spaces in a string
String String::removeSpaces()
{
String temp = ""; //empty string that will hold the given string but without spaces
int index = 0; //index for adding the non-spaces to the new string
for (int i = 0; i < this->size(); i ++)
{
if (this->data[i] != 32)
{
temp.data[index] = this->data[i];
index++;
}
}
temp.data[index] = '\0'; //assign final character to null-terminator
temp.len = index; //length does NOT include the null-terminator
temp.cap = temp.len + 2; //capacity is 2 more than the length
return temp;
}
//count the characters occurring in this string
int String::countCharacters(char letter)
{
int count = 0;
for (int i = 0; i < this->size(); i++)
{
if (this->data[i] == letter)
{
count++;
}
}
return count;
}
//---------------------------- END MISCELLANEOUS FUNCTION SECTION -------------------------------------
//--------------------------------- END STRING CLASS --------------------------------------------------