-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSString.cpp
222 lines (185 loc) · 4.98 KB
/
DSString.cpp
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
//
// Created by Sammy Timmins on 9/1/20.
//
#include "DSString.h"
DSString::DSString() //default constructor
{
data = new char[1];
length = 0;
capacity = 1;
}
DSString::DSString(const char *word) //constructor is passed a char* array
{
capacity = strlen(word) + 1;
length = strlen(word);
data = new char[capacity];
strcpy(data, word);
}
DSString::DSString(const DSString ©) //copy constructor
{
capacity = strlen(copy.data) + 1;
length = strlen(copy.data);
data = new char[capacity];
strcpy(data, copy.data);
}
DSString::~DSString() //destructor
{
delete [] data;
}
DSString &DSString::operator=(const DSString ©) { //= operator overload for when DSString set to another DSString
if(this != ©)
{
if(this->data != nullptr) //ensures no memory leak occurs in the case of reassignment
{
delete [] this->data;
this->data = nullptr;
}
this->capacity = copy.capacity;
this->length = copy.length;
this->data = new char[copy.capacity];
strcpy(data, copy.data);
}
return *this;
}
DSString &DSString::operator=(const char *word) { //= operator overload for when DSString set to char*
delete [] data;
data = nullptr;
capacity = strlen(word) + 1;
length = strlen(word);
data = new char[capacity];
strcpy(data, word);
return *this;
}
DSString DSString::operator+(const DSString ©) const {//+ operator overloaded to add DSStrings together
DSString newString;
newString.length = this->length + copy.length;
newString.capacity = this->capacity + copy.capacity - 1;
newString.data = new char[newString.capacity];
strcpy(newString.data, this->data);
newString.data = strcat(newString.data, copy.data);
newString.data[newString.capacity - 1] = '\0';
return newString;
}
DSString DSString::operator+(const char *word) //+ operator overload for when DSString is added to a char*
{
DSString newString;
newString.length = this->length + strlen(word);
newString.capacity = this->capacity + strlen(word);
newString.data = new char[newString.capacity];
strcpy(newString.data, this->data);
newString.data = strcat(newString.data, word);
newString.data[newString.capacity - 1] = '\0';
return newString;
}
DSString& DSString::operator+(const char character) //+ operator overload for when DSString is added to a char
{
char *temp = new char[capacity];
length += 1;
capacity += 1;
for(int i = 0; i < capacity - 1; i++)
{
temp[i] = data[i];
}
delete [] data;
data = new char[capacity];
for(int i = 0; i < length; i++)
{
data[i] = temp[i];
}
data[length - 1] = character;
data[capacity - 1] = '\0';
delete [] temp;
return *this;
}
bool DSString::operator==(const char *word){ //equality operator overload to compare a DSString to a char*
if(strcmp(data, word) == 0)
{
return true;
} else{
return false;
}
}
bool DSString::operator==(const DSString &compare) { //equality operator overload to comapre a DSString to another DSString
if(strcmp(data, compare.data) == 0)
{
return true;
} else{
return false;
}
}
bool DSString::operator>(const DSString &compare) { //> operator overload to compare a DSString to a DSString
if(strcmp(data, compare.data) > 0)
{
return true;
} else{
return false;
}
}
bool DSString::operator<(const DSString &compare) const{ //< operator overload to compare a DSString to a DSString
if(strcmp(data, compare.data) < 0)
{
return true;
} else{
return false;
}
}
char& DSString::operator[](const int x) const { //[] operator overload to access a single char in a DSString
return data[x];
}
ostream& operator<<(ostream& os, const DSString& word) //outstream operator for DSString objects
{
int outputLength = strlen(word.data);
for(int i = 0; i < outputLength; i++)
{
os << word.data[i];
}
return os;
}
void DSString::print() //print function to display the data held in DSString
{
int printLength = strlen(data);
for(int i = 0; i < printLength; i++)
{
cout << data[i];
}
cout << endl;
}
int DSString::getCapacity() //returns capacity
{
return capacity;
}
int DSString::getLength() const //returns length
{
return length;
}
char* DSString::getString() //returns the data string
{
return data;
}
DSString DSString::substr(int pos, int npos) const //returns a DSString of the indexes passed to it
{
DSString toReturn;
char *temp = new char[npos - pos + 1];
int j = 0;
for(int i = pos; i < npos; i++)
{
temp[j] = this->data[i];
j++;
}
temp[j] = '\0';
toReturn = temp;
delete [] temp;
return toReturn;
}
char* DSString::c_str() //converts the DSString to a cstring
{
return data;
}
int DSString::dsstoi()
{
return stoi(data);
}
float DSString::dsstof()
{
return stof(data);
}