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:
strlen
: Calculates the length of a string
const char* myStr = "Hello, world!"; int length = 0; while (*myStr != '\0') { length++; myStr++; } cout << "Length: " << length << endl;
In this example, we define a character array myStr
and use a while loop to iterate through the string character by character. Using pointer arithmetic, we increment the pointer str
until we reach the end of the string (the null character \0
). We then print the length of the string to the console.
strcpy
: Copies a string to another string
const char* strOne = "Hello, world!"; char strTwo[50]; char* ptr = strTwo; while (*strOne != '\0') { *ptr = *strOne; ptr++; strOne++; } *ptr = '\0'; cout << "Copied string: " << strTwo << endl;
In this example, we define a character array str1
and a character array str2
with enough space to hold the copied string. We also define a pointer ptr
and initialize it to point to the beginning of str2
.
Using a while loop, we iterate through str1
character by character and use pointer arithmetic to copy each character to str2
using the dereference operator (*
). Finally, we add the null character (\0
) to the end of str2
to terminate the string.
strcat
: Concatenates two strings
#include <iostream> using namespace std; int main() { // Define a character array "strOne" and initialize it with "Hello, " char strOne[50] = "Hello, "; // Define a constant character pointer "strTwo" and assign //it the address of the string "world!" const char* strTwo = "world!"; // 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') { // Assign the character pointed to by "strTwo" //to the character pointed to by "ptrString" *ptrString = *strTwo; // Increment the pointer "ptrString" to point to the // next memory location ptrString++; // Increment the pointer "strTwo" to point to the next // character in the string 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; return 0; }
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.