-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-string.cpp
256 lines (210 loc) · 5.63 KB
/
c-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
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
#include <iostream>
using namespace std;
class cstring {
char *a;
size_t length;
public:
size_t _strlen(const char *); //get the length of the string
char *_strcpy(char *dest, const char *src); //copy the [src] string into the [dest] string, return [dest] pointer
char *_strcat(char *dest, const char *src); //append the [src] string to the [dest] string, return [dest] pointer
size_t size() const; //get length of the string
bool isEmpty() const; //return true if the string is null
void empty(); //set the string to null
cstring &concate(const cstring &); //concate two strings, result is stored into [this] string
cstring &concate(const char *); //overloading
char *get_str() const; //return the inner pointer
cstring(); //default constructor
cstring(char); //trivial constructor
cstring(const char *); //trivial constructor
cstring(const cstring &); //copy constructor
cstring(cstring &&); //move copy constructor
~cstring();
cstring &operator=(const char *str);
cstring &operator=(const cstring &str);
cstring &operator=(char *&&str);
cstring &operator=(cstring &&str) noexcept;
cstring &operator+=(const char *str);
cstring &operator+=(const cstring &str);
cstring operator+(const cstring &str);
char &operator[](std::size_t n);
friend ostream &operator<<(ostream &os, const cstring &str);
friend istream &operator>>(istream &is, cstring &str);
friend cstring operator+(char &ch, cstring &str);
};
class cstring;
size_t cstring::_strlen(const char *str) {
if (str == nullptr) return 0;
size_t cnt = 0;
auto p = str;
while ((*p) != '\0') {
cnt++;
p++;
}
return cnt;
}
char *cstring::_strcpy(char *dest, const char *src) {
auto p1 = dest;
if (dest == nullptr || dest == NULL) {
p1 = dest = new char[this->_strlen(src) + 1];
}
auto p2 = src;
while (p2 != NULL && (*p2) != '\0') {
*p1 = *p2;
p1++;
p2++;
}
*p1 = 0;
return dest;
}
char *cstring::_strcat(char *dest, const char *src) {
char *now = new char[this->_strlen(dest) + 1];
this->_strcpy(now, dest);
delete[] dest;
dest = new char[this->_strlen(now) + this->_strlen(src) + 1];
auto p1 = dest;
auto p2 = src;
auto p3 = now;
while ((*p3) != '\0') {
*p1 = *p3;
p1++;
p3++;
}
while ((*p2) != '\0') {
*p1 = *p2;
p2++;
p1++;
}
*p1 = 0;
return dest;
}
size_t cstring::size() const {
return this->length;
}
bool cstring::isEmpty() const {
return this->length == 0;
}
void cstring::empty() {
delete (this->a);
this->a = nullptr;
this->length = 0;
}
cstring &cstring::concate(const cstring &str) {
this->a = this->_strcat(this->a, str.a);
this->length += str.length;
return *this;
}
cstring &cstring::concate(const char *str) {
this->_strcat(this->a, str);
this->length += this->_strlen(str);
return *this;
}
char *cstring::get_str() const {
return this->a;
}
cstring::cstring() {
this->length = 0;
this->a = nullptr;
}
cstring::cstring(char x) {
this->a = new char[2];
this->a[0] = x;
this->a[1] = 0;
this->length = 1;
}
cstring::cstring(const char *str) {
this->a = new char[this->_strlen(str) + 1];
this->_strcpy(this->a, str);
this->length = this->_strlen(this->a);
}
cstring::cstring(const cstring &str) {
this->a = new char[this->_strlen(str.a) + 1];
this->_strcpy(this->a, str.a);
this->length = this->_strlen(this->a);
}
cstring::cstring(cstring &&str) {
this->a = str.a;
this->length = str.length;
str.a = nullptr;
str.length = 0;
}
cstring::~cstring() {
delete[] this->a;
}
cstring &cstring::operator=(const char *str) {
this->a = this->_strcpy(this->a, str);
return *this;
}
cstring &cstring::operator=(const cstring &str) {
this->a = this->_strcpy(this->a, str.a);
}
cstring &cstring::operator+=(const char *str) {
this->a = this->_strcat(this->a, str);
}
cstring &cstring::operator+=(const cstring &str) {
this->a = this->_strcat(this->a, str.a);
}
cstring cstring::operator+(const cstring &str) {
cstring ret;
ret.a = ret._strcat(ret.a, this->a);
ret.a = ret._strcat(ret.a, str.a);
return std::move(ret);
}
char &cstring::operator[](std::size_t n) {
return this->a[n];
}
istream &operator>>(istream &is, cstring &str) {
int maxcnt = 2; // char[] size
char *tmp = new char[maxcnt + 2];
int cnt = 0;
char ch;
cstring *ret;
while ((ch = is.get()) && (ch != ' ' && ch != '\n')) {
tmp[cnt++] = ch;
if (cnt == maxcnt) {
maxcnt <<= 1;
char *nxt = new char[maxcnt + 2];
tmp[cnt] = 0;
nxt = str._strcpy(nxt, tmp);
swap(nxt, tmp);
delete[] nxt;
}
}
tmp[cnt++] = 0;
str.a = tmp;
return is;
}
ostream &operator<<(ostream &os, const cstring &str) {
os << str.a;
return os;
}
cstring &cstring::operator=(char *&&str) {
this->a = str;
this->length = this->_strlen(str);
str = nullptr;
return *this;
}
cstring &cstring::operator=(cstring &&str) noexcept {
this->a = str.a;
this->length = str.length;
str.a = nullptr;
return *this;
}
cstring operator+(char &ch, cstring &str) {
cstring ret(ch);
return std::move(ret + str);
}
//DO NOT CHANGE ANYTHING BELOW!!!
cstring f(const char *s) {
return cstring(s);
}
int main() {
cstring s1, s2;
char buf1[128], buf2[128];
cin >> s1 >> buf1 >> buf2;
cstring s3(buf2);
s2 = s1;
s2 += f(buf1);
s1 = s3[5] + s2;
cout << s1 << endl;
return 0;
}