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* str = "Hello, world!"; int length = 0; while (*str != '\0') { length++; str++; } cout << "Length: " << length << endl;
In this example, we define a character array str
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* str1 = "Hello, world!"; char str2[50]; char* ptr = str2; while (*str1 != '\0') { *ptr = *str1; ptr++; str1++; } *ptr = '\0'; cout << "Copied string: " << str2 << 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() { char strOne[50] = "Hello, "; // Define a character array "strOne" and initialize it with "Hello, " const char* strTwo = "world!"; // Define a constant character pointer "strTwo" and assign it the address of the string "world!" char* ptrString = strOne + strlen(strOne); // Declare a character pointer "ptrString" and initialize it with the address of the null terminator of "strOne" while (*strTwo != '\0') { *ptrString = *strTwo; // 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: " << 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:
#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.