-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Main.cpp
More file actions
207 lines (172 loc) · 4.53 KB
/
String_Main.cpp
File metadata and controls
207 lines (172 loc) · 4.53 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
// ===========================================================================
// String_Main.cpp
// ===========================================================================
#include <iostream>
#include "String.h"
static void testCtorsDtor()
{
// test c'tors
String s1;
std::cout << "s1: " << s1 << std::endl;
String s2("12345");
std::cout << "s2: " << s2 << std::endl;
String s3(s2);
std::cout << "s3: " << s3 << std::endl;
}
static void testInsert()
{
String s1("12678");
s1.insert("345", 2);
std::cout << "s1: " << s1 << std::endl;
String s2("ABCD");
s2.insert(s1, 2);
std::cout << "s2: " << s2 << std::endl;
s2.insert("!", 13);
std::cout << "s2: " << s2 << std::endl;
s2.insert(".", 12);
std::cout << "s2: " << s2 << std::endl;
s2.insert(".", 0);
std::cout << "s2: " << s2 << std::endl;
}
static void testAppend()
{
String s1;
s1.append("123");
std::cout << "s1: " << s1 << std::endl;
String s2("ABC");
s2.append("DEF");
std::cout << "s2: " << s2 << std::endl;
}
static void testRemove()
{
String s1("12345");
s1.remove(1, 3);
std::cout << "s1: " << s1 << std::endl;
String s2("ABC");
s2.remove(0, 3);
std::cout << "s2: " << s2 << std::endl;
}
static void testSubString()
{
String s2("ABCDE");
String s;
s = s2.subString(1, 3);
std::cout << "s: " << s << std::endl;
s = s2.subString(0, 5);
std::cout << "s: " << s << std::endl;
s = s2.subString(0, 6);
std::cout << "s: " << s << std::endl;
s = s2.subString(0, 0);
std::cout << "s: " << s << std::endl;
}
static void testOperators()
{
// test '=' operator
String s1;
String s2("12345");
s1 = s2;
std::cout << "s1: " << s1 << std::endl;
// test '==' operator
std::cout << "s1 == s2: " << (s1 == s2) << std::endl;
s1.remove(0, 1);
std::cout << "s1 == s2: " << (s1 == s2) << std::endl;
std::cout << "s1 != s2: " << (s1 != s2) << std::endl;
}
static void testInput()
{
String s;
std::cout << "Enter string: ";
std::cin >> s;
std::cout << "String: " << s << '.' << std::endl;
}
static void testLeftRight()
{
String s("12345");
String s1;
String s2;
s1 = s.left(3);
std::cout << "Left(3): " << s1 << '.' << std::endl;
s2 = s.right(2);
std::cout << "Right(2): " << s2 << '.' << std::endl;
s1 = s.left(5);
std::cout << "Left(5): " << s1 << '.' << std::endl;
s2 = s.right(5);
std::cout << "Right(5): " << s2 << '.' << std::endl;
s1 = s.left(6);
std::cout << "Left(6): " << s1 << '.' << std::endl;
s2 = s.right(6);
std::cout << "Right(6): " << s2 << '.' << std::endl;
}
static void testToUpperToLower()
{
String s("aBcDeFgHiJkLmNoPqRsTuVwXyZ");
std::cout << "s:" << s << '.' << std::endl;
s.toUpper();
std::cout << "s:" << s << '.' << std::endl;
s.toLower();
std::cout << "s:" << s << '.' << std::endl;
}
static void testFind()
{
String s("ABCDEFGHIJKLMN");
int i = s.find("IJK");
std::cout << "i: " << i << std::endl;
i = s.find("ABCDEFGHIJKLMN");
std::cout << "i: " << i << std::endl;
i = s.find("IJKZ");
std::cout << "i: " << i << std::endl;
i = s.find("N");
std::cout << "i: " << i << std::endl;
i = s.find("Z");
std::cout << "i: " << i << std::endl;
i = s.find("");
std::cout << "i: " << i << std::endl;
}
static void testSubsriptOperator()
{
String s("ABCDE");
std::cout << "s[0]: " << s[0] << std::endl;
std::cout << "s[4]: " << s[4] << std::endl;
s[0] = '<';
s[4] = '>';
std::cout << "s: " << s << std::endl;
try
{
s[5] = '!';
}
catch (const std::out_of_range& ex)
{
std::cout << ex.what() << std::endl;
}
}
static void testStringConcatenation()
{
String s1("123");
String s2("ABC");
String s3;
s3 = s1 + s2;
std::cout << "s1:" << s1 << std::endl;
std::cout << "s2:" << s2 << std::endl;
std::cout << "s3:" << s3 << std::endl;
s1 += s2 += "789";
std::cout << "s1:" << s1 << std::endl;
std::cout << "s2:" << s2 << std::endl;
}
void exerciseClassString()
{
testCtorsDtor();
testInsert();
testAppend();
testRemove();
testSubString();
testOperators();
testInput();
testLeftRight();
testToUpperToLower();
testFind();
testSubsriptOperator();
testStringConcatenation();
}
// ===========================================================================
// End-of-File
// ===========================================================================