forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pezy chen
committed
Jun 1, 2018
1 parent
4c5ce5c
commit eb1c9c5
Showing
1 changed file
with
12 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |