...
Code Block | ||
---|---|---|
| ||
#include <iostream> using namespace std; int main() { char strOne[50] = "Hello, "; // Define a character array "strOne" and initialize it with "Hello, " const char* strTwostrOne[50] = "world!Hello, "; // Define a constant character pointer "strTwo" and assign //it the address of the string "world!" const char* ptrStringstrTwo = strOne"world!"; + strlen(strOne); // Declare a character pointer "ptrString" and initialize it with //the address of the null terminator of "strOne" char* ptrString = strOne + strlen(strOne); while (*strTwo != '\0') { *ptrString = *strTwo; // Assign the character pointed to by "strTwo" //to the character pointed to by "ptrString" ptrString++*ptrString = *strTwo; // Increment the pointer "ptrString" to point to the next memory location // next strTwo++;memory location ptrString++; // Increment the pointer "strTwo" to point to the next character in the string // }character in the string *ptrString = '\0'; strTwo++; } // Add a null terminator at the end of the concatenated string // to indicate its end *ptrString = '\0'; // Output the concatenated string cout << "Concatenated string: " << strOne << endl; // Output the concatenated string return 0; } |
The given code demonstrates string concatenation using character arrays and pointers. Here's a breakdown of the code:
...