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
|
#include "String.h"
void String::CopyFrom(const String& other) {
str = new char[strlen(other.str) + 1];
strcpy(str, other.str);
len = other.len;
}
void String::Free() {
delete[] str;
}
String::String() {
str = new char[1];
str[0] = '\0';
len = 0;
}
inline String::String(char ch)
{
str = new char[2];
str[0] = ch;
str[1] = '\0';
len = 1;
}
String::String(const char* str) {
len = strlen(str);
this->str = new char[len + 1];
strcpy(this->str, str);
}
String::String(const String& other) {
CopyFrom(other);
}
String& String::operator=(const String& other) {
if (this != &other) {
Free();
CopyFrom(other);
}
return *this;
}
String::~String() {
Free();
}
int String::getLenght() const {
return len;
}
String String::SubString(int start) {
if (start < 0 || start > len)
throw std::logic_error("Invalid index.\n");
char* newStr = new char[len - start + 2];
int j = 0;
for (int i = start; i < len; i++)
newStr[j++] = str[i];
newStr[j] = '\0';
String newString(newStr);
delete[] newStr;
return newString;
}
String String::SubString(int start, int end) {
if (start < 0 || start > len || end <= 0 || end > len)
throw std::logic_error("Invalid index.\n");
char* newStr = new char[end - start + 2];
int j = 0;
for (int i = start; i <= end; i++)
newStr[j++] = str[i];
newStr[j] = '\0';
String newString(newStr);
delete[] newStr;
return newString;
}
int String::IndexOf(const String& other, int index) {
if (index < 0)
throw std::logic_error("Invalid index.\n");
int iter = 0;
for (int i = index; i <= len; i++) {
if (iter == other.len)
return i - other.len;
if (str[i] != other.str[iter])
iter = 0;
else
iter++;
}
return -1;
}
int String::IndexOf(const char* str, int index) {
String searched(str);
return IndexOf(searched, index);
}
int String::CountOcurences(String str)
{
int count = 0;
int i = IndexOf(str, 0);
while (i != -1)
{
count++;
i = IndexOf(str, i + 1);
}
return count;
}
inline int String::GetOcurence(String str, int num)
{
int count = 0;
int i = IndexOf(str, 0);
while (i != -1)
{
count++;
if (count == num)
return i;
i = IndexOf(str, i + 1);
}
return count;
}
const char& String::operator[](int index) const {
if (index >= len)
throw std::out_of_range("Out of range!");
return str[index];
}
char& String::operator[](int index) {
return str[index];
}
String& String::operator+=(const String& other) {
char* temp = str;
str = new char[len + other.len + 1];
strcpy(str, temp);
strcat(str, other.str);
len += other.len;
delete[] temp;
return *this;
}
bool operator==(const String& lhs, const String& rhs) {
return !strcmp(lhs.str, rhs.str);
}
bool operator!=(const String& lhs, const String& rhs) {
return strcmp(lhs.str, rhs.str);
}
std::ostream& operator<<(std::ostream& os, const String& obj) {
return os << obj.str;
}
std::istream& operator>>(std::istream& is, String& obj) {
char buff[MAX_SIZE];
is.getline(buff, MAX_SIZE);
obj.str = new char[strlen(buff) + 1];
strcpy(obj.str, buff);
return is;
}
const String operator+(const String& lhs, const String& rhs) {
if (lhs.getLenght() == 0)
return rhs;
else if (rhs.getLenght() == 0)
return lhs;
return String(lhs) += rhs;
}
|