-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.h
162 lines (133 loc) · 3.17 KB
/
String.h
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
//
// Created by NewUser on 2021/11/10.
//
#ifndef STRING_STRING_H
#define STRING_STRING_H
#include <cstring>
class String : public string {
protected:
int Length;
char *Strp;
public:
String() {
Strp = nullptr;
Length = 0;
}
String(const char *s);
String(const String &s);
~String() {
delete[]Strp;
}
void show() {
cout << Strp << endl;
}
String &operator=(String &string);
friend String &operator+(const String &string1, const String &string2);
friend String &operator-(const String &string1, const String &string2);
const char *IsIn(const char &c) const;
bool IsSubStr(const char *str) const;
bool operator>(const String &string1) const;
bool operator<(const String &string1) const;
bool operator==(const String &string1) const;
operator const char *() const {
return (const char *) Strp;
}
int GetLen() {
return Length;
}
const char *GetString() {
return Strp;
}
};
String::String(const char *s) {
if (s) {
Length = strlen(s);
Strp = new char[strlen(s) + 1];
strcpy(Strp, s);
} else {
Length = 0;
Strp = nullptr;
}
}
String::String(const String &s) {
if (s.Strp) {
Length = s.Length;
Strp = new char[Length + 1];
strcpy(Strp, s.Strp);
} else {
Length = 0;
Strp = nullptr;
}
}
String &String::operator=(String &string) {
if (&string == this) {
return *this;
}
if (Strp) {
delete[]Strp;
}
if (string.Strp) {
Length = string.Length;
Strp = new char[Length + 1];
strcpy(Strp, string.Strp);
} else {
Length = 0;
Strp = nullptr;
}
return *this;
}
String &operator+(const String &string1, const String &string2) {
String *temp;
temp = new String;
temp->Length = string1.Length + string2.Length;
temp->Strp = new char[temp->Length + 1];
strcpy(temp->Strp, string1.Strp);
strcat(temp->Strp, string2.Strp);
return *temp;
}
String &operator-(const String &string1, const String &string2) {
String *temp;
char *p, *q;
int i = 0;
p = string1.Strp;
temp = new String;
temp->Length = string1.Length - string2.Length + 1;
q = strstr(p, string2.Strp);
if (q) {
temp->Strp = new char[temp->Length];
while (p < q) {
temp->Strp[i++] = *p;
p++;
}
p += string2.Length;
while ((temp->Strp[i++] = *p++));
}
return *temp;
}
const char *String::IsIn(const char &c) const {
char *p;
p = Strp;
while (*p) {
if (*p++ == c) {
return --p;
}
}
return nullptr;
}
bool String::IsSubStr(const char *str) const {
if (strstr(Strp, str)) {
return true;
} else {
return false;
}
}
bool String::operator>(const String &string1) const {
return strcmp(Strp, string1.Strp) > 0;
}
bool String::operator<(const String &string1) const {
return strcmp(Strp, string1.Strp) < 0;
}
bool String::operator==(const String &string1) const {
return strcmp(Strp, string1.Strp) == 0;
}
#endif //STRING_STRING_H