-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_prac.cpp
More file actions
120 lines (96 loc) · 2.13 KB
/
Copy pathstring_prac.cpp
File metadata and controls
120 lines (96 loc) · 2.13 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
#include <iostream>
using namespace std;
class String {
public:
String();
String(const char* str);
~String();
String(const String& str);
String operator+(const String& str) const;
bool operator==(const String& str) const;
char& operator[](int n) const;
size_t size() const;
const char* c_str() const;
String& operator=(const String& str);
String& operator+=(const String& str);
friend istream& operator>>(istream& is, String& str);
friend ostream& operator<<(ostream& os, String& str);
private:
char *data;
size_t length;
};
String::String():length(0), data(NULL) {}
String::String(const char* str) {
if (str == NULL) {
length = 0;
data = new char[1];
*data = '\0';
} else {
length = strlen(str);
data = new char[length+1];
strcpy(data, str);
}
}
String::~String() {
delete []data;
length = 0;
}
String::String(const String& str) {
length = str.size();
data = new char[length+1];
strcpy(data, str.c_str());
}
String String::operator+(const String& str) const {
String temp;
temp.length = length+str.size();
temp.data = new char[temp.length+1];
strcpy(temp.data, data);
strcat(temp.data, str.data);
return temp;
}
bool String::operator==(const String& str) const {
if (str.size() != length)
return false;
return strcmp(data, str.data) ? false : true;
}
char& String::operator[](int n) const {
if (n >= length)
return *(data+length-1);
return *(data+n);
}
size_t String::size() const {
return length;
}
const char* String::c_str() const {
return data;
}
String& String::operator=(const String& str) {
if (this == &str)
return *this;
delete []data;
length = str.length;
data = new char[length+1];
strcpy(data, str.c_str());
return *this;
}
String& String::operator+=(const String& str) {
length += str.length;
char* temp = new char[length+1];
strcpy(temp, data);
strcat(temp, str.data);
delete[] data;
data = temp;
return *this;
}
istream& operator>>(istream& is, String& str) {
char temp[1000];
is >> temp;
str.length = strlen(temp);
str.data = new char[str.length+1];
strcpy(str.data, temp);
return is;
}
ostream& operator<<(ostream& os, String& str) {
os << str.data;
return os;
}