Why can’t my loop detect symbol in const string?

#include<iostream>
#include<string>
#include<sstream>
#include <typeinfo>
#include<cmath>
#include<vector>
#include <algorithm>

using namespace std;

class Mystring {
    char *arr;
public:
    Mystring(const char pointer[]) {
        int i = 0;
        while (pointer[i] != '') {
            i++;
            cout << pointer[i] << endl;
        }
        arr = new char[i];
        i = 0;
        while (pointer[i] != '') {
            arr[i] = pointer[i];
            i++;
        }
    }

    friend ostream& operator<<(ostream& out, Mystring& str) {
        int i = 0;
        while (str.arr[i] != '') {
            out << str.arr[i];
            i++;
        }
        return out;
    }
};

int main() {
    Mystring string("Hello, world!");
    cout << string << endl;
}

I’m trying to create my own string class. The length of “hello world!” is 13, but the length of arr turns out to be 17. It is filled with some strange chars for some reason I don’t understand. When I’m trying to cout string it returns this: Hello, world!¤¤¤¤.

Answer

You forgot to add '' at the end of your arr[].

Moreover, the size or arrshould be increased to incorporate this , as noted in @user7860670’s answer.

Moreover, using string as a variable name, in addition to using namespace std;, is confusing at least.

#include<iostream>
#include<string>
#include<sstream>
#include <typeinfo>
#include<cmath>
#include<vector>
#include <algorithm>

//using namespace std;

class Mystring {
    char *arr;
public:
    Mystring(const char pointer[]) {
        int i = 0;
        while (pointer[i] != '') {
            i++;
            std::cout << pointer[i] << std::endl;
        }
        arr = new char[i+1];
        i = 0;
        while (pointer[i] != '') {
            arr[i] = pointer[i];
            i++;
        }
        arr[i] = '';
    }

    friend std::ostream& operator<<(std::ostream& out, const Mystring& str) {
        int i = 0;
        while (str.arr[i] != '') {
            out << str.arr[i];
            i++;
        }
        return out;
    }
};

int main() {
    Mystring mstring("Hello, world!");
    std::cout << mstring << std::endl;
}