-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyString.cpp
84 lines (73 loc) · 1.6 KB
/
MyString.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
#include "MyString.h"
using namespace std;
MyString::MyString()
{
content = new char[1];
content[0] = '\0';
}
MyString::MyString(const char* input)
{
int len = strlen(input);
this->content = new char[len + 1];
strcpy(this->content, input);
this->content[len] = '\0';
}
MyString::MyString(const MyString& from)
{
int len = strlen(from.content);
this->content = new char[len + 1];
strcpy(this->content, from.content);
this->content[len] = '\0';
}
MyString& MyString::operator=(const MyString& from)
{
if (this != &from)
{
delete[] content;
int len = strlen(from.content);
this->content = new char[len + 1];
strcpy(this->content, from.content);
this->content[len] = '\0';
}
return *this;
}
MyString::~MyString()
{
delete[] this->content;
}
char* MyString::getContent() const
{
return this->content;
}
void MyString::append(char to_append)
{
int len = strlen(this->content);
char* new_content = new char[len + 2];
strcpy(new_content, this->content);
new_content[len] = to_append;
new_content[len + 1] = '\0';
delete[] this->content;
this->content = new_content;
}
void MyString::print()
{
std::cout << this->content << std::endl;
}
std::ostream& operator <<(std::ostream& out, const MyString& string)
{
for (int i = 0; string.content[i] != '\0'; i++)
{
out << string.content[i];
}
return out;
}
std::istream& operator >>(std::istream& in, MyString& string)
{
char c;
do
{
cin.get(c);
string.append(c);
} while (c != '\n');
return in;
}