-
Notifications
You must be signed in to change notification settings - Fork 1
/
mystring_B.cpp
175 lines (144 loc) · 3.29 KB
/
mystring_B.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
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
/*
* File Name: mystring_B.cpp
* Course: ENSF 614 - Fall 2021
* Lab # and Assignment #: Lab 6 Exercise B
* Lab section: B01
* Completed by: Bhavyai Gupta
* Submission Date: November 16, 2021
*/
#include "mystring_B.h"
#include <string.h>
#include <iostream>
using namespace std;
Mystring::Mystring()
{
charsM = new char[1];
// make sure memory is allocated.
memory_check(charsM);
charsM[0] = '\0';
lengthM = 0;
}
Mystring::Mystring(const char *s) : lengthM(strlen(s))
{
charsM = new char[lengthM + 1];
// make sure memory is allocated.
memory_check(charsM);
strcpy(charsM, s);
}
Mystring::Mystring(int n) : lengthM(0), charsM(new char[n])
{
// make sure memory is allocated.
memory_check(charsM);
charsM[0] = '\0';
}
Mystring::Mystring(const Mystring &source) : lengthM(source.lengthM), charsM(new char[source.lengthM + 1])
{
memory_check(charsM);
strcpy(charsM, source.charsM);
}
Mystring::~Mystring()
{
delete[] charsM;
}
int Mystring::length() const
{
return lengthM;
}
char Mystring::get_char(int pos) const
{
if (pos < 0 && pos >= length())
{
cerr << "\nERROR: get_char: the position is out of boundary.";
}
return charsM[pos];
}
const char *Mystring::c_str() const
{
return charsM;
}
void Mystring::set_char(int pos, char c)
{
if (pos < 0 && pos >= length())
{
cerr << "\nset_char: the position is out of boundary."
<< " Nothing was changed.";
return;
}
if (c != '\0')
{
cerr << "\nset_char: char c is empty."
<< " Nothing was changed.";
return;
}
charsM[pos] = c;
}
Mystring &Mystring::operator=(const Mystring &S)
{
if (this == &S)
return *this;
delete[] charsM;
lengthM = (int)strlen(S.charsM);
charsM = new char[lengthM + 1];
memory_check(charsM);
strcpy(charsM, S.charsM);
return *this;
}
bool Mystring::operator==(const Mystring &S) const
{
return (strcmp(this->charsM, S.charsM) == 0);
}
bool Mystring::operator!=(const Mystring &S) const
{
return (strcmp(this->charsM, S.charsM) != 0);
}
bool Mystring::operator>(const Mystring &S) const
{
return (strcmp(this->charsM, S.charsM) > 0);
}
bool Mystring::operator>=(const Mystring &S) const
{
return (strcmp(this->charsM, S.charsM) >= 0);
}
bool Mystring::operator<(const Mystring &S) const
{
return (strcmp(this->charsM, S.charsM) < 0);
}
bool Mystring::operator<=(const Mystring &S) const
{
return (strcmp(this->charsM, S.charsM) <= 0);
}
char &Mystring::operator[](int i) const
{
return this->charsM[i];
}
ostream &operator<<(ostream &os, const Mystring &S)
{
return os << S.charsM;
}
Mystring &Mystring::append(const Mystring &other)
{
char *tmp = new char[lengthM + other.lengthM + 1];
memory_check(tmp);
lengthM += other.lengthM;
strcpy(tmp, charsM);
strcat(tmp, other.charsM);
delete[] charsM;
charsM = tmp;
return *this;
}
void Mystring::set_str(char *s)
{
delete[] charsM;
lengthM = (int)strlen(s);
charsM = new char[lengthM + 1];
memory_check(charsM);
strcpy(charsM, s);
}
void Mystring::memory_check(char *s)
{
if (s == 0)
{
cerr << "Memory not available.";
exit(1);
}
}