In C++, strings are represented as arrays of characters, and can be manipulated using pointers and pointer arithmetic.
Here are some code snippets of common string functions that use pointers:
...
strcat
: Concatenates two strings
Code Block | ||
---|---|---|
| ||
#include <iostream> using namespace std; int main() { char str1strOne[50] = "Hello, "; // Define a character array "strOne" and initialize it with "Hello, " const char* str2strTwo = "world!"; // Define a constant character pointer "strTwo" and assign it the address of the string "world!" char* ptrptrString = str1strOne + strlen(str1); strOne); // Declare a character pointer "ptrString" and initialize it with the address of the null terminator of "strOne" while (*str2strTwo != '\0') { *ptrptrString = *str2; ptr++; str2++; } *ptrstrTwo; // Assign the character pointed to by "strTwo" to the character pointed to by "ptrString" ptrString++; // Increment the pointer "ptrString" to point to the next memory location strTwo++; // Increment the pointer "strTwo" to point to the next character in the string } *ptrString = '\0'; // Add a null terminator at the end of the concatenated string cout << "Concatenated string: " << str1strOne << endl; // Output the concatenated string return 0; } |
In this example, we define a character array str1
and a character array str2
. We also define a pointer ptr
and initialize it to point to the end of str1
, using the strlen
function to calculate the length of str1
.
Using a while loop, we iterate through str2
character by character and use pointer arithmetic to copy each character to the end of str1
using the dereference operator (*
). Finally, we add the null character (\0
) to the end of str1
to terminate the concatenated string.
...
The given code demonstrates string concatenation using character arrays and pointers. Here's a breakdown of the code:
#include <iostream>
: This line includes the necessary header file for input/output operations in C++.using namespace std;
: This line allows the use of standard C++ functions and objects without specifying the namespace.int main() {
: The program's entry point, the main function.char strOne[50] = "Hello, ";
: This declares a character array namedstrOne
with a size of 50 and initializes it with the string "Hello, ".const char* strTwo = "world!";
: This declares a constant character pointer namedstrTwo
and assigns it the address of the string "world!".char* ptrString = strOne + strlen(strOne);
: This declares a character pointer namedptrString
and initializes it with the address of the null terminator ofstrOne
. This allows for concatenation at the end ofstrOne
.while (*strTwo != '\0') {
: This initiates a loop that iterates until the end of the stringstrTwo
is reached (null terminator).*ptrString = *strTwo;
: This assigns the character pointed to bystrTwo
to the character pointed to byptrString
, effectively concatenatingstrTwo
to the end ofstrOne
.ptrString++;
: This increments the pointerptrString
to point to the next memory location.strTwo++;
: This increments the pointerstrTwo
to point to the next character in the string.*ptrString = '\0';
: This adds a null terminator at the end of the concatenated string to indicate its end.cout << "Concatenated string: " << strOne << endl;
: This outputs the concatenated string stored instrOne
using thecout
object.return 0;
: The main function finishes, and the program terminates with a return value of 0.
...