Write Your Own String Class:

class OwnString
{

private :

char *string1 = NULL;
int size;

public:
OwnString(void)
{
string1 = NULL;
}

OwnString(const char * strData)
{
size = strlen(strData);
string1  = new char[size + 1];
int i = 0;
while (*strData)
{
string1[i] = *strData;

strData++;
i++;
}
}
OwnString & operator=(OwnString & obj)
{
if (this != &obj)
{
delete[]string1;
size = obj.size;
string1 = new char[size+1];
int i = 0;
while (i<size+1)
{
string1[i] = obj.string1[i];

//strData++;
i++;
}
}
return *this;
}

OwnString (const OwnString & obj)
{
if (this != &obj)
{
delete[]string1;
size = obj.size;
string1 = new char[size + 1];
int i = 0;
while (i < size + 1)
{
string1[i] = obj.string1[i];

//strData++;
i++;
}
}
}

void print()
{
for (int i = 0; i < size; i++)
{
cout << string1[i];
}

cout << endl;
}

~OwnString()
{
delete string1;
}
};

Comments

Popular posts from this blog

Dynamic Memory allocation in C++ for 1d, 2d and 3d array

Printing the odd and even no using Pthread