forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex12_23.cpp
35 lines (30 loc) · 839 Bytes
/
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
//
// ex12_23.cpp
// Exercise 12.23
//
// Created by pezy on 12/30/14.
// Updated by sanerror on 11/9/16.
// Copyright (c) 2014 pezy. All rights reserved.
//
// 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>
#include <string>
#include <cstring>
int main() {
const char *c1 = "Hello ";
const char *c2 = "World";
unsigned len = strlen(c1) + strlen(c2) + 1;
char *r = new char[len]();
strcat_s(r, len, c1);
strcat_s(r, len, c2);
std::cout << r << std::endl;
std::string s1 = "Hello ";
std::string s2 = "World";
strcpy_s(r, len, (s1 + s2).c_str());
std::cout << r << std::endl;
delete[] r;
return 0;
}