-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.cpp
More file actions
211 lines (177 loc) · 3.47 KB
/
str.cpp
File metadata and controls
211 lines (177 loc) · 3.47 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#define _CRT_SECURE_NO_WARNINGS
#include "str.h"
#include <iostream>
#include <string.h>
using namespace std;
//C'tor && D'tor
//C'tor -Q1
Str::Str()
{
m_str = new char[5];
strcpy(m_str, "none");
}
//Copy C'tor -Q2
Str::Str(const Str& other) : m_str(NULL)
{
*this = other;
}
//C'tor - get string -Q3
Str::Str(const char* str) : m_str(NULL)
{
*this = str;
}
//D'tor -Q4
Str::~Str()
{
if(this->m_str != nullptr)
delete[] m_str;
}
//Metods - Opertors
//Operator for comparing two strings -Q5
bool Str::operator==(const Str& other) const
{
bool res = false;
int x = strcmp(m_str, other.m_str);
if (x == 0)
{
res = true;
}
return res;
}
//Operator for comparing two strings -Q6
bool Str::operator!=(const Str& other) const
{
bool res = false;
int x = strcmp(m_str, other.m_str);
if (x != 0)
{
res = true;
}
return res;
}
//Compares two lexicographic strings -Q7
bool Str::operator>(const Str& other) const
{
bool res = false;
int x = strcmp(m_str, other.getStr());
if (x > 0)
{
res = true;
}
return res;
}
//Compares two lexicographic strings -Q8
bool Str::operator<(const Str& other) const
{
bool res = false;
int x = strcmp(m_str, other.getStr());
if (x < 0)
{
res = true;
}
return res;
}
//Object placement operator -Q9
const Str& Str::operator=(const Str& other)
{
if (this != &other)
{
delete[] m_str;
m_str = new char[strlen(other.getStr()) + 1];
strcpy(m_str, other.getStr());
}
return *this;
}
//String placement operator -Q10
const Str& Str::operator=(const char* str)
{
if (this->m_str != str)
{
delete[] m_str;
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);
}
return *this;
}
//Returns the character instead of the index -Q11
char& Str::operator[](int index) const
{
return m_str[index];
}
//Askit promotes each character by 1 -Q12
const Str& Str::operator++()
{
int i = 0;
while (m_str[i] != '\0')
{
m_str[i]++;
i++;
}
return *this;
}
//Askit promotes each character by 1 -Q13
Str Str::operator++(int)
{
Str temp(*this);
int i = 0;
while (m_str[i] != '\0')
{
m_str[i]++;
i++;
}
return temp;
}
//Casting Operator - Returns the length of the string -Q14
Str::operator int() const
{
int lenght = strlen(m_str);
return lenght;
}
//Returns the first position of the character ch in a string or (-1) if not found -Q15
int Str::operator()(char ch) const
{
int size = strlen(m_str);
for (int i = 0; i < size; i++)
{
if (m_str[i] == ch)
{
return i;
}
}
return -1;
}
//Threads between strings -Q16
Str Str::operator+(const Str& other) const
{
Str tmp;
tmp.m_str = new char[strlen(this->m_str) + strlen(other.m_str) + 1];
strcpy(tmp.m_str, this->m_str);
strcat(tmp.m_str, other.m_str);
return tmp;
}
//Threads between strings - Q19
const Str& Str::operator+=(const Str& other)
{
*this = *this + other;
return *this;
}
//Friend Operators
//Global function - Threads between strings -Q17
Str operator+(const char* str, const Str& other)
{
Str tmp;
tmp.m_str = new char[strlen(str) + strlen(other.m_str) + 1];
strcpy(tmp.m_str, str);
strcat(tmp.m_str, other.m_str);
return tmp;
}
//Global function - Threads the string that gets as a parameter, num times -Q18
Str operator*(int num, const Str& other)
{
Str tmp("");
for (int i = 0; i < num; i++)
{
tmp = tmp + other;
}
return tmp;
}