Versions Compared

Key

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

...

Code Block
languagecpp
const char* strmyStr = "Hello, world!";
int length = 0;

while (*strmyStr != '\0') {
    length++;
    strmyStr++;
}

cout << "Length: " << length << endl;

In this example, we define a character array str 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.

...

Code Block
languagecpp
const char* str1strOne = "Hello, world!";
char str2strTwo[50];

char* ptr = str2strTwo;
while (*str1strOne != '\0') {
    *ptr = *str1strOne;
    ptr++;
    str1strOne++;
}
*ptr = '\0';

cout << "Copied string: " << str2strTwo << 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.

...