#include#include using namespace std; int main() { char s1[] = "Hello"; // a character array without null termination char s2[] = " World!"; // a character array without null termination int len_s1 = strlen(s1); // find length of s1 without null termination int len_s2 = strlen(s2); // find length of s2 without null termination int total_len = len_s1 + len_s2; // total length of concatenated string without null termination char s3[total_len+1]; // allocate memory for concatenated string with null termination strcpy(s3, s1); // copy s1 to s3 strcat(s3, s2); // append s2 to s3 cout << s3 << endl; // print concatenated string with null termination return 0; }
#includeIn the example above, we concatenate two string objects, s1 and s2, to form a new string object called s3. We use the + operator to concatenate two strings. Finally, we print the concatenated string using the cout function. Package library: Standard C++ Library (STL)#include using namespace std; int main() { string s1 = "Hello"; // a string object string s2 = "World!"; // a string object string s3 = s1 + s2; // concatenate two strings cout << s3 << endl; // print concatenated string return 0; }