-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_test.cc
More file actions
71 lines (58 loc) · 1.77 KB
/
Copy pathstring_test.cc
File metadata and controls
71 lines (58 loc) · 1.77 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
#include <cassert>
#include <iostream>
using namespace std;
void replace_all(string& s, const string& from, const string& to) {
string::size_type pos = 0;
while(true) {
// Find next occurance of the needle
pos = s.find(from, pos);
// If the needle wasn't found exit the loop
if(pos == string::npos) {
break;
}
// Replace the needle with the new content
s.replace(pos, from.size(), to);
// Set the search position after the new content
pos += to.size();
}
}
string primes(int ceiling) {
// Init string with P and mark 0 and 1 as not primes
string primeString = string(ceiling, 'P');
primeString[0] = 'C';
primeString[1] = 'C';
// Replace every mulitple with C
for(size_t i = 2; i < primeString.size(); ++i) {
size_t j = 2;
while(i*j < primeString.size()) {
primeString[i*j] = 'C';
++j;
}
}
// Return resulting string
return primeString;
}
int print_largest_prime(int ceiling) {
// Get string with primes and return the position of the last occurance of 'P'
string pri = primes(ceiling);
return pri.rfind('P');
}
int main() {
cout << "Testing replace_all." << endl;
string text = "A man, a plan, a canal, Panama!";
replace_all(text, "an", "XXX");
assert(text == "A mXXX, a plXXX, a cXXXal, PXXXama!");
text = "ananan";
replace_all(text, "an", "XXX");
assert(text == "XXXXXXXXX");
text = "ananan";
replace_all(text, "an", "anan");
assert(text == "anananananan");
cout << "Success: All assertions return true." << endl << endl;
int nPrimesToPrint = 200;
cout << "Printing primes from 0 to " << nPrimesToPrint << endl;
cout << primes(nPrimesToPrint) << endl << endl;
int largestPrimeCeiling = 100000;
cout << "Printing largest prime less than " << largestPrimeCeiling << endl;
cout << print_largest_prime(largestPrimeCeiling) << endl;
}