-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyString.cpp
More file actions
186 lines (132 loc) · 2.71 KB
/
Copy pathMyString.cpp
File metadata and controls
186 lines (132 loc) · 2.71 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
#include "MyString.h"
#include <cstring>
#include <stdexcept>
int MyString::stringLength(const char* s) const
{
int len = 0;
while (s[len] != '\0')
len++;
return len;
}
void MyString::copyString(char* dest, const char* src) const
{
int i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
int MyString::compareString(const char* s1, const char* s2) const
{
int i = 0;
while (s1[i] != '\0' && s2[i] != '\0')
{
if (s1[i] != s2[i])
return s1[i] - s2[i];
i++;
}
return s1[i] - s2[i];
}
MyString::MyString()
{
length = 0;
str = new char[1];
str[0] = '\0';
}
MyString::MyString(const char* s)
{
length = stringLength(s);
str = new char[length + 1];
copyString(str, s);
}
MyString::MyString(const MyString& other)
{
length = other.length;
str = new char[length + 1];
copyString(str, other.str);
}
MyString::~MyString()
{
delete[] str;
}
MyString& MyString::operator=(const MyString& other)
{
if (this != &other)
{
delete[] str;
length = other.length;
str = new char[length + 1];
copyString(str, other.str);
}
return *this;
}
MyString MyString::operator+(const MyString& other) const
{
MyString temp;
delete[] temp.str;
temp.length = length + other.length;
temp.str = new char[temp.length + 1];
int i;
for (i = 0; i < length; i++)
temp.str[i] = str[i];
for (int j = 0; j < other.length; j++)
temp.str[i++] = other.str[j];
temp.str[i] = '\0';
return temp;
}
bool MyString::operator==(const MyString& other) const
{
return compareString(str, other.str) == 0;
}
bool MyString::operator!=(const MyString& other) const
{
return !(*this == other);
}
char& MyString::operator[](int index)
{
if (index < 0 || index >= length)
throw out_of_range("Index out of range");
return str[index];
}
const char& MyString::operator[](int index) const
{
if (index < 0 || index >= length)
throw out_of_range("Index out of range");
return str[index];
}
int MyString::size() const
{
return length;
}
bool MyString::empty() const
{
return length == 0;
}
void MyString::clear()
{
delete[] str;
length = 0;
str = new char[1];
str[0] = '\0';
}
const char* MyString::c_str() const
{
return str;
}
ostream& operator<<(ostream& out, const MyString& s)
{
out << s.str;
return out;
}
istream& operator>>(istream& in, MyString& s)
{
char buffer[1000];
in >> buffer;
delete[] s.str;
s.length = s.stringLength(buffer);
s.str = new char[s.length + 1];
s.copyString(s.str, buffer);
return in;
}