Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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:

  1. #include <iostream>: This line includes the necessary header file for input/output operations in C++.

  2. using namespace std;: This line allows the use of standard C++ functions and objects without specifying the namespace.

  3. int main() {: The program's entry point, the main function.

  4. char strOne[50] = "Hello, ";: This declares a character array named strOne with a size of 50 and initializes it with the string "Hello, ".

  5. const char* strTwo = "world!";: This declares a constant character pointer named strTwo and assigns it the address of the string "world!".

  6. char* ptrString = strOne + strlen(strOne);: This declares a character pointer named ptrString and initializes it with the address of the null terminator of strOne. This allows for concatenation at the end of strOne.

  7. while (*strTwo != '\0') {: This initiates a loop that iterates until the end of the string strTwo is reached (null terminator).

  8. *ptrString = *strTwo;: This assigns the character pointed to by strTwo to the character pointed to by ptrString, effectively concatenating strTwo to the end of strOne.

  9. ptrString++;: This increments the pointer ptrString to point to the next memory location.

  10. strTwo++;: This increments the pointer strTwo to point to the next character in the string.

  11. *ptrString = '\0';: This adds a null terminator at the end of the concatenated string to indicate its end.

  12. cout << "Concatenated string: " << strOne << endl;: This outputs the concatenated string stored in strOne using the cout object.

  13. return 0;: The main function finishes, and the program terminates with a return value of 0.

  • No labels