-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
322 lines (266 loc) · 6.8 KB
/
string.cpp
File metadata and controls
322 lines (266 loc) · 6.8 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
#include "String.h"
//CONSTRUCTORS + DESTRUCTOR
//Default constructor.
String::String() : _str{ nullptr } {
_str = new char[1];
_str[0] = '\0';
}
//Single parameter constructor.
String::String(const char* str) {
size_t length = strlen(str);
_str = new char[length + 1];
strcpy(_str, str);
}
//Copy constructor.
String::String(const String& other) : _str{nullptr} {
if (this == &other) return;
size_t length = other.Length();
_str = new char[length + 1];
strcpy(_str, other._str);
}
//Move constructor.
String::String(String&& other) noexcept : _str(nullptr) {
_str = other._str;
other._str = nullptr;
}
//Wacky constructor???
String::String(AdoptPointer, char* str) : _str{str} {
}
//Destructor.
String::~String() {
delete[] _str;
}
//OPERATOR OVERLOADS
//Equal to comparison operator == overload.
bool String::operator==(const String& other) const {
int a;
a = strcmp(_str, other._str);
if (a == 0) return true;
else return false;
}
//Not equal to comparison operator != overload.
bool String::operator!=(const String& other) const {
int a;
a = strcmp(_str, other._str);
if (a == 0) return false;
else return true;
}
//Less than operator < overload.
bool String::operator<(const String& other) const {
int a = strcmp(_str, other._str);
if (a < 0) return true;
else return false;
}
//Greater than operator > overload.
bool String::operator>(const String& other) const {
int a = strcmp(_str, other._str);
if (a > 0) return true;
else return false;
}
//Copy assignment operator = overload.
String& String::operator=(const String& str) {
if (this == &str) return *this;
delete[] _str;
size_t length = str.Length();
_str = new char[length + 1];
strcpy(_str, str._str);
return *this;
}
//Move assignment operator = overload.
String& String::operator=(String&& other) noexcept {
if (this != &other) {
delete[] _str;
_str = other._str;
other._str = nullptr;
}
return *this;
}
//Member access operator [] overload.
char& String::operator[](size_t index) {
return _str[index];
}
//Member access operator [] overload.
const char& String::operator[](size_t index) const {
return _str[index];
}
//Arithmetic operator + overload.
String String::operator+(const String& other) const {
size_t length = this->Length() + other.Length();
String buffer(AdoptPointer{}, new char[length + 1] {});
strcpy(buffer._str, _str);
strcat(buffer._str, other._str);
return buffer;
}
//Assignment operator += overload.
String& String::operator+=(const String& other) {
size_t length = this->Length() + other.Length();
char* str = new char[length + 1] {};
strcpy(str, _str);
strcat(str, other._str);
delete[] _str;
_str = str;
return *this;
}
//Insertion operator << overload.
std::ostream& operator<<(std::ostream& out, const String& str) {
return out << str.CStr();
}
//Extraction operator >> overload.
std::istream& operator>>(std::istream& in, String& str) {
str.ReadFromConsole();
return in;
}
//MEMBER FUNCTIONS
//Get length of string.
size_t String::Length() const {
size_t length;
length = strlen(_str);
return length;
}
//Finds character at specified index. Returns null if outside of index range.
char& String::CharacterAt(size_t index) {
size_t length = this->Length();
if (index > length) return _str[length];
return _str[index];
}
//Finds character at specified index. Returns null if outside of index range.
const char& String::CharacterAt(size_t index) const {
size_t length = this->Length();
if (index > length) return _str[length];
return _str[index];
}
//Checks if strings have the same characters. Returns true if equal; returns false if not equal.
bool String::EqualTo(const String& other) const {
int a;
a = strcmp(_str, other._str);
if (a == 0) return true;
return false;
}
//Concatenates another string to the end of the string.
String& String::Append(const String& str) {
char* temp = _str;
size_t length = strlen(temp) + str.Length() + 1;
_str = new char[length];
strcpy(_str, temp);
strcat(_str, str._str);
delete[] temp;
return *this;
}
//Concatenates another string to the front of the string.
String& String::Prepend(const String& str) {
char* temp = _str;
size_t length = strlen(temp) + str.Length() + 1;
_str = new char[length];
strcpy(_str, str._str);
strcat(_str, temp);
delete[] temp;
return *this;
}
//Return char array for console.
const char* String::CStr() const {
return _str;
}
//Returns string with all characters in lower case.
String& String::ToLower() {
for (int i = 0; i < this->Length(); i++) {
this->_str[i] = std::tolower(this->_str[i]);
}
return *this;
}
//Returns string with all characters in upper case.
String& String::ToUpper() {
for (int i = 0; i < this->Length(); i++) {
this->_str[i] = std::toupper(this->_str[i]);
}
return *this;
}
//Finds a substring in a string and returns it's location.
size_t String::Find(const String& str) const {
size_t s1Len = this->Length();
size_t s2Len = str.Length();
for (size_t i = 0; i < s1Len; i++) {
size_t j;
for (j = 0; j < s2Len; j++) {
if (_str[i + j] != str[j]) {
break;
}
}
if (j == s2Len) {
return i;
}
}
return -1;
}
//Finds a substring from the specified index of a string and returns it's location.
size_t String::Find(size_t startIndex, const String& str) const {
size_t s1Len = this->Length();
size_t s2Len = str.Length();
for (size_t i = startIndex; i < s1Len; i++) {
size_t j;
for (j = 0; j < s2Len; j++) {
if (_str[i + j] != str[j]) {
break;
}
}
if (j == s2Len) {
return i;
}
}
return -1;
}
//Replace all occurences of the first string with the second string.
String& String::Replace(const String& find, const String& replace) {
bool isReplacing = true;
while (isReplacing) {
int diff = find.Length() - replace.Length();
size_t rPos = this->Find(find);
if (rPos == -1) break;
String buff = _str;
delete[] _str;
_str = new char[buff.Length() + diff + 1];
strcpy(_str, buff._str);
_str[rPos] = '\0';
strcat(_str, replace._str);
_str[rPos + replace.Length() + 1] = '\0';
strcat(_str, buff._str + rPos + find.Length());
}
return *this;
}
//Convert input from console to string.
String& String::ReadFromConsole() {
char c;
std::cin.get(c);
std::cin.putback(c);
std::streamsize size = std::cin.rdbuf()->in_avail();
char* temp = new char[size] {};
std::cin.readsome(temp, size);
temp[size - 1] = '\0';
delete[] _str;
_str = temp;
&std::ostream::flush;
return *this;
}
//Output string to console.
const String& String::WriteToConsole() const {
std::cout << _str;
return *this;
}
//Alternates letters into upper and lower.
String& String::Wobble() {
srand(time(nullptr));
int r = rand() % 2;
this->ToLower();
if (r == 0) {
for (int i = 0; i < this->Length(); i++) {
_str[i] = toupper(_str[i]);
i++;
}
}
else if (r == 1) {
for (int i = 1; i < this->Length(); i++) {
_str[i] = toupper(_str[i]);
i++;
}
}
return *this;
}