Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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
languagecpp
#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:

  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.

...