0

How to combine arrays in c++?

There are two character arrays, for example a{'a','b','c'} and b{'d','e','f'}, you need to get an array c{'a','b','c','d','e','f'} ,how can I do this?

2nd Dec 2021, 9:23 AM
Анатолий Скворцов
Анатолий Скворцов - avatar
5 odpowiedzi
+ 4
#include <iostream> #include <cstring> using namespace std; #define SIZE 100 int main() { char a[SIZE] = {'a','b','c'}; char b[SIZE] = {'d','e','f'}; char c[SIZE] = {}; size_t i; for(i=0; i<strlen(a); i++) c[i] = a[i]; for(i=0; i<strlen(b); i++) c[i + strlen(a)] = b[i]; cout << c << endl; return 0; } // Keep learning & happy coding :D
2nd Dec 2021, 11:50 AM
SoloProg
SoloProg - avatar
+ 2
SoloProg good solution. Alternative way using C string functions: strcpy(c, a); strcat(c, b); cout << c << endl;
2nd Dec 2021, 6:48 PM
Brian
Brian - avatar
+ 1
Pranshu Sachan , I don't know about your code show me your code in a correct way.
2nd Dec 2021, 5:39 PM
SoloProg
SoloProg - avatar
+ 1
👍👍
2nd Dec 2021, 6:52 PM
SoloProg
SoloProg - avatar
0
SoloProg i think size(c) >=size(a) +size(b). If we want to accumulate all value of a and b into c
2nd Dec 2021, 12:01 PM
Pranshu Sachan