generated from coleca24/csce240_f21_p2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperString.cpp
199 lines (176 loc) · 4.68 KB
/
SuperString.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2021 Tatiana Washington
#include "./SuperString.h"
// DO NOT MODIFY START
void SuperString::print() {
if (size != 0) {
for (int i = 0; i < size; i++) {
std::cout << data[i];
}
std::cout << "\n";
} else {
std::cout << "<EMPTY>\n";
}
}
char SuperString::get(int index) {
if (index >= 0 && index < size) {
return data[index];
}
return '\0';
}
int SuperString::length() {
return size;
}
// DO NOT MODIFY END
// PUT YOUR CODE BELOW!
SuperString::SuperString() { //default constructor
size = 0; //set size 0
data = new char[size]; //allocate data to new char of size
}
SuperString::SuperString(std::string str) { //alt constructor #1
size = str.length(); //set size to length of str
data = new char[size];
for (int i = 0; i < size; i++) { //fill all contents of array w/ str
data[i] = str[i];
}
}
SuperString::SuperString(int size, char val) { //alt constructor #2
(*this).size = size; //set size to size
data = new char[size];
for (int i = 0; i < size; i++) { //fill all contents of array w/ val
data[i] = val;
}
}
SuperString::SuperString(const SuperString& copy) { //copy constructor
size = copy.size; //set size to size of obj
data = new char[size];
for (int i = 0; i < size; i++) { //fill all contents of array w/ data
data[i] = copy.data[i];
}
}
int SuperString::find(char c, int start) { //findchar
for (int i = start; i < 10; i++) { //return index of 1st instance of char
if (data[i] == c)
{
return i;
}
}
return -1;
}
int SuperString::find(std::string str, int start) { //find str
int L = str.length();
int R = 4;
int END = R - L + 1;
for (int i = start; i >= END; i++) { //return index of 1st instance of char
for ( int j = 0; i < L; i++) {
if (data[i + j] != str[j]) {
break;
}
}
if(int j = L) {
return i;
}
}
return -1;
}
SuperString SuperString::substr(int start, int numChar) { //substrValid
int L = start + numChar;
if (L > size) {
return SuperString(); //contain obj data starting at index start
}
std::string s1;
for (int i = start; i < L; i++) {
s1.insert(i, s1);
}
return *this; //return obj
}
SuperString SuperString::reverse() { //reverse
int S = 4;
for (int i = 0; i < S/2; i++) { //return obj that contains calling obj data in reverse order
int temp = data[i];
data[i] = data[S-i-1];
data[S-i-1] = temp;
}
return *this;
}
SuperString SuperString::replace(int start, int numChar, std::string sub) { //replaceValid
int sn = start + numChar;
if (sn > size) {
return SuperString(); // has contents of data after replacement
}
std::string s1;
for (int i = 0; i < start; i++) {
s1.insert(i, s1);
}
return *this; //return obj w/ size 0 if invalid index pass
}
void SuperString::push_back(char c) { //pushback
char* temp = data;
temp = new char[size+1];
for (int i = 0; i < size; i++) { //adds new char to data
temp[i] = data[i];
}
temp[size] = c;
delete data;
data = temp;
size = size + 1;
return;
}
void SuperString::append(std::string str) { //appendstring
int L = str.length();
int newSize = size + L;
char* temp = data;
temp = new char[size+1];
for ( int i = 0; i < size; i++) { //append the content to the end of data
temp[i] = data[i];
}
for (int i = 0; i < L; i++) {
temp[size+i] = str[i];
}
delete data;
data = temp;
size = newSize;
return;
}
void SuperString::append(const SuperString& obj) { //appendsuperstring
int L = obj.size;
int newSize = size + L;
char* temp = data;
temp = new char[size+1];
for ( int i = 0; i < size; i++) { //append content to the end of data
temp[i] = data[i];
}
for (int i = 0; i < newSize; i++) {
temp[i] = obj.data[i-size];
}
delete data;
data = temp;
size = newSize;
return;
}
void SuperString::replace(char find, char rep) { //replace
char temp = (*this).find(find, 0);
while (temp != -1) { //replace all char in data with rep
data[temp] = rep;
temp = (*this).find(find,temp+1);
}
return;
}
//extra credit
void SuperString::removeAll(char c) {
return;
}
// Bonus Methods
void SuperString::replace(std::string find, std::string rep) {
return;
}
bool SuperString::isUpper() {
return true;
}
bool SuperString::isLower() {
return true;
}
bool SuperString::isTitleCase() {
return true;
}
SuperString::~SuperString() {
}