-
Notifications
You must be signed in to change notification settings - Fork 33
/
12.23.cpp
34 lines (31 loc) · 826 Bytes
/
12.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
/*
* 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.
*
* By Faisal Saadatmand
*/
#include <iostream>
#include <memory>
#include <string>
#include <cstring>
int main()
{
// string literals
const char *s1 = "conca";
const char *s2 = "tnate";
size_t size = strlen(s1) + strlen(s2);
char *ps = new char[size + 1]; // add 1 for '\0'
std::strcpy(ps, s1);
std::strcat(ps, s2);
auto ch = ps;
while (*ch) // *p != '\0'
std::cout << *ch++;
std::cout << '\n';
delete [] ps;
// library strings
auto lstring = std::string("conca") + std::string("tnate");
std::cout << lstring << std::endl;
return 0;
}