-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.cpp
230 lines (205 loc) · 3.89 KB
/
String.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
223
224
225
226
227
228
229
230
#include "String.h"
#include <cstring>
String::String()
{
capacity = 8;
size = 0;
text = new char[capacity];
text[0] = '\0';
}
String::String(const char* _string)
{
this->capacity = strlen(_string) + 1;
this->text = new char[capacity];
strcpy_s(this->text, capacity, _string);
this->size = strlen(_string);
}
String::~String()
{
this->erase();
}
String::String(const String& _string)
{
this->copy(_string);
}
String& String::operator=(const String& _string)
{
if (this != &_string)
{
this->erase();
this->copy(_string);
}
return *this;
}
String& String::operator=(const char* text)
{
this->erase();
this->capacity = strlen(text) + 1;
this->text = new char[capacity];
this->size = strlen(text);
strcpy_s(this->text, capacity, text);
return *this;
}
bool String::operator==(const String& _string) const
{
return !strcmp(this->text, _string.text);
}
bool String::operator==(const char* text) const
{
/*if (this->getSize() != strlen(text))
return false;*/
for (size_t i = 0; i < size; ++i)
{
if (text[i] != this->text[i])
{
return false;
}
}
return true;
}
bool String::operator!=(const String& _string) const
{
return strcmp(this->text, _string.text);
}
bool String::operator<(const String otherString) const
{
bool changed = false;
for (size_t i = 0; i < this->size; ++i)
{
if (this->text[i] >= 65 && this->text[i] <= 90)
{
changed = true;
this->text[i] += 32;
}
if (otherString.text[i] >= 65 && otherString.text[i] <= 90)
otherString.text[i] += 32;
if (this->text[i] < otherString.text[i])
{
if(changed){ this->text[i] -= 32; }
return true;
}
if (this->text[i] > otherString.text[i])
{
if (changed) { this->text[i] -= 32; }
return false;
}
if (changed) { this->text[i] -= 32; }
}
if(this->size == otherString.size)
return false;
return true;
}
String String::operator+(String& other)
{
String temp;
temp.copy(*this);
for (size_t i = 0; i < other.size; ++i)
{
temp += other[i];
}
return temp;
}
const char String::operator[](int index) const
{
return this->text[index];
}
String& String::operator+=(const char _element)
{
if (this->size + 1 >= this->capacity)
{
this->resize();
}
this->text[size] = _element;
this->size++;
this->text[size] = '\0';
return *this;
}
size_t String::getSize() const
{
return this->size;
}
const char* String::getText() const
{
return this->text;
}
void String::print() const
{
std::cout << this->text << std::endl;
std::cout << this->capacity << std::endl;
std::cout << this->size << std::endl;
}
void String::removeLast()
{
if (this->size >= 1)
{
this->size--;
char* newString = new char[capacity];
for (size_t i = 0; i < size; ++i)
{
newString[i] = text[i];
}
delete[] text;
this->text = newString;
this->text[size] = '\0';
}
}
int String::StringToNumber()
{
int numbersCount = 0;
for (size_t i = 0; i < this->size; ++i)
{
if (this->text[i] >= '0' && this->text[i] <= '9')
{
numbersCount++;
}
}
int position = pow(10,numbersCount - 1);
int number = 0;
for (size_t i = 0; i < this->size; ++i)
{
if (this->text[i] >= '0' && this->text[i] <= '9')
{
number += (text[i]-48)*position ;
position /= 10;
}
}
return number;
}
void String::copy(const String& _string)
{
this->capacity = _string.capacity;
this->size = _string.size;
this->text = new char[this->capacity];
strcpy_s(this->text, this->capacity, _string.text);
}
void String::erase()
{
delete[] this->text;
}
void String::resize()
{
this->capacity *= 2;
char* newString = new char[capacity];
for (size_t i = 0; i < size; ++i)
{
newString[i] = text[i];
}
delete[] text;
this->text = newString;
}
std::ostream& operator<<(std::ostream& out, const String& _string)
{
out << _string.text;
return out;
}
std::istream& operator>>(std::istream& in,String& _string)
{
char element = ' ';
while (element != '\n')
{
element = in.get();
_string += element;
}
_string.removeLast();
return in;
}