-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
89 lines (77 loc) · 2.23 KB
/
main.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
#include <bits/stdc++.h>
using namespace std;
void string_manipulation_iterator (string &s, const string &oldVal, const string &newVal)
{
for (auto iter {s.begin()}; iter < s.end(); ++iter)
{
if (*iter == oldVal.front())
{
for (int i {1}; i != oldVal.size(); ++i)
{
if (*(iter + i) != oldVal.at (i))
{
break;
}
if (i + 1 == oldVal.size())
{
auto pos = distance (s.begin(), iter);
s.erase (pos, oldVal.size());
s.insert (pos, newVal);
iter = s.begin() + pos + int (newVal.size());
}
}
}
}
}
void string_manipulation_index_and_replace (string &s, const string &oldVal, const string &newVal)
{
for (int i {0}; i < s.size(); ++i)
{
if (s.at (i) == oldVal.at (0))
{
for (int j {1}; j != oldVal.size(); ++j)
{
if (s.at (i + j) != oldVal.at (j))
{
break;
}
if (j + 1 == oldVal.size())
{
s.replace (i, oldVal.size(), newVal);
}
}
}
}
}
string &prefix_and_suffix (string &name, const string &prefix, const string &suffix)
{
name.insert (0, prefix);
// name.append(suffix);
name.insert (distance (name.begin(), name.end()), suffix);
return name;
}
int main (int argc, char *argv[])
try
{
string s {"there was tho tho and thru"};
cout << s << endl;
string_manipulation_iterator (s, "tho", "though");
cout << s << endl;
string_manipulation_iterator (s, "thru", "through");
cout << s << endl;
cout << "---------------------" << endl;
s = "there was tho tho and thru";
cout << s << endl;
string_manipulation_index_and_replace (s, "tho", "though");
cout << s << endl;
string_manipulation_index_and_replace (s, "thru", "through");
cout << s << endl;
cout << "---------------------" << endl;
string s1 = "Anna";
cout << prefix_and_suffix (s1, "Ms. ", ", The III Queen of Mars.");
return 0;
}
catch (exception &e)
{
cerr << "Error: " << e.what() << endl;
}