Skip to content

Commit

Permalink
Fixed pezy#142
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy chen committed Jun 1, 2018
1 parent 4c5ce5c commit eb1c9c5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions ch03/ex3_40.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
#include <iostream>
// Write a program to define two character arrays initialized
// from string literals. Now define a third character array to hold the
// concatenation of the two arrays. Use `strcpy` and `strcat` to copy the two
// arrays into the third.

#include <cstring>
#include <iostream>

using std::cout;
using std::endl;

int main()
{
constexpr size_t new_size = strlen(cstr1) + strlen(" ") + strlen(cstr2) + 1;
char cstr3[new_size];
const char cstr1[] = "Hello";
const char cstr2[] = "world!";

size_t new_size = strlen(cstr1) + strlen(" ") + strlen(cstr2) + 1;
char* cstr3 = new char[new_size];

strcpy(cstr3, cstr1);
strcat(cstr3, " ");
strcat(cstr3, cstr2);

std::cout << cstr3 << std::endl;
delete [] cstr3;
}

0 comments on commit eb1c9c5

Please sign in to comment.