forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex12.23.cpp
132 lines (106 loc) · 2.99 KB
/
ex12.23.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
/***************************************************************************
* @file The code is for the exercises in C++ Primmer 5th Edition
* @author Alan.W
* @date 26 DEC 2013
* @remark
***************************************************************************/
//!
//! Exercise 12.23:
//! Write a program to concatenate two string literals, putting the result in a
//! dynamically allocated array of char.
//! Write a program to concatenate two library strings that have the same value
//! as the literals used in the first program.
//!
#include <iostream>
/**
* @brief concatenate two string literals and return it stored in dynamic memory.
* @attention dont forget free it using delete[].
*/
char *
concat_litral(const char * const s1, const char * const s2);
/**
* @brief concatenate two string literals and return it stored in dynamic memory.
* @attention dont forget free it using delete[].
*/
char*
concat_litral(const std::string& s1, const std::string& s2);
int main()
{
/**
* @brief test for 12.23 part 1.
*/
/*
char s1[80], s2[40];
std::cin >> s1 >> s2;
char * d_str= concat_litral(s1, s2);
std::cout << d_str;
delete [] d_str;
*/
/**
* @brief test for 12.23 part 2.
*/
std::string s1, s2;
std::cin >> s1 >> s2;
char * d_str= concat_litral(s1, s2);
std::cout << d_str;
delete [] d_str;
return 0;
}
/**
* @brief concatenate two string literals and strore it in dynamic memory and return it
* @attention dont forget free it using delete[].
* @note note the 'const' used in this function.
*/
char*
concat_litral(const char * const s1, const char * const s2)
{
//! allocate the dynamic array and define a movable pointer to it
char* const result = new char[sizeof(s1) + sizeof(s2) + 10];
char* m_result = result;
//! define the movable pointers to s1 and s2
const char* m_s1 = s1;
const char* m_s2 = s2;
//! copy s1 to result
while(*m_s1 != '\0')
{
*m_result = *m_s1;
++m_result;
++m_s1;
}
//! copy s2 to result
while(*m_s2 != '\0')
{
*m_result = *m_s2;
++m_result;
++m_s2;
}
//! append '\0' at the end
*m_result = '\0';
return result;
}
/**
* @brief concatenate two library string and return it stored in dynamic memory.
* @note library can be iterated through using for range .
*/
char*
concat_litral(const std::string &s1, const std::string &s2)
{
//! allocate the dynamic array and define a movable pointer to it
char* const result = new char[sizeof(s1) + sizeof(s2) + 10];
char* m_result = result;
//! copy s1 to result using for range
for (const auto &c : s1)
{
*m_result = c;
++m_result;
}
//! append s2 to result using for range
for (const auto &c : s2)
{
*m_result = c;
++m_result;
}
//! append '\0' at the end
*m_result = '\0';
return result;
}