-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path9.46.cpp
40 lines (34 loc) · 1006 Bytes
/
9.46.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
/*
* Exercise 9.46: Rewrite the previous exercise using a position and length to
* manage the strings. This time use only the insert function.
*
* By Faisal Saadatmand
*/
#include <iostream>
#include <string>
std::string &formatName(std::string &, const std::string &,
const std::string &, const std::string &);
std::string &formatName(std::string &name, const std::string &prefix1,
const std::string &prefix2, const std::string &suffix)
{
if (!prefix1.empty())
name.insert(0, " ").insert(0, prefix1);
if (!prefix2.empty())
name.insert(0, " ").insert(0, prefix2);
if (!suffix.empty())
name.insert(name.size(), " ").insert(name.size(), suffix);
return name;
}
int main()
{
std::string name1{"Bjarne Stroustrup"};
std::string name2{"Elizabeth"};
std::string name3{"Hanah Arendt"};
formatName(name1, "Dr.", "", "");
formatName(name2, "Queen","", "II");
formatName(name3, "Prof.", "", "");
std::cout << name1 << '\n'
<< name2 << '\n'
<< name3 << '\n';
return 0;
}