0

I want to add elements in the array of class MyString like this

void print(const MyString &obj)
{
    obj.print();
}

int main()
{
    MyString str1; // make a default string of 100 size 
    str1.add('[') ;//insert at position 1 or index 0 
    str1.add('A'); //insert at position 2 or index 1 
    str1.add('B'); //insert at position 3 or index 2 
    str1.add('C'); //insert at position 4 or index 3
    str1.add('D'); //insert at position 5 or index 4 
    str1.add('E'); //insert at position 6 or index 5 
    str1.add('F'); //insert at position 7 or index 6 
    str1.add('G'); //insert at position 8 or index 7 
    str1.add('h'); //insert at position 9 or index 8 
    str1.add('i'); //insert at position 10 or index 9
    str1.add('j'); //insert at position 11 or index 10 
    str1.add('k'); //insert at position 12 or index 11 
    str1.add('l'); //insert at position 13 or index 12 
    str1.add('m'); //insert at position 14 or index 13 
    str1.add('n'); //insert at position 15 or index 14 
    str1.add('o'); //insert at position 16 or index 15
    str1.add('p'); //insert at position 17 or index 16 
    str1.add(']'); //insert at position 18 or index 17
    print(str1);
}

Wrote a function which would take value as a parameter and insert it in the array

void MyString::add(char value)
{
    char * temp = new char[strlen(this->arr) + 1];
    for (int i = 0; i < strlen(this->arr); i++)
    {
        temp[i] = this->arr[i];
    }

    temp[strlen(this->arr)] = value;
    delete[]this->arr;
    this->arr = temp;
    temp = nullptr;
}

and a print function to display the elements in array

void MyString::print() const
{
    cout << this->arr << endl;
}

But the code doesn't display anything during debugging.. Where am I getting it wrong?

scypx
  • 67
  • 7
  • why are you not using a `std::vector` (or even a `std::string`)? Your implementation is quite ineffecient, allocating and deallocating memory each `add`. and running `strlen` 3 times is also quite inefficient... – JHBonarius Dec 03 '20 at 14:14
  • 2
    `strlen` requires a `'\0'` terminated array of char. – Eljay Dec 03 '20 at 14:15
  • 1
    Does this answer your question? [What is a null-terminated string?](https://stackoverflow.com/questions/2037209/what-is-a-null-terminated-string) – Stephen Newell Dec 03 '20 at 14:16
  • 1
    `strlen(this->arr) + 1` should probably be `+ 2` to account for the null terminating char. It also looks like you neglect to place that null char in there as a terminator at all actually. – scohe001 Dec 03 '20 at 14:16
  • @scohe001 Can I allocate memory to char temp pointer using a fixed size instead of using string length of the array? – scypx Dec 03 '20 at 14:23
  • @scypx I'm not sure I understand what you're asking. If you want the same fixed size allocated to your array every time, just use an integer literal in your `new` (`new char[7]`). – scohe001 Dec 03 '20 at 14:26
  • If you "make a default string of 100 size" then why do you reallocate for each character you add? – Some programmer dude Dec 03 '20 at 14:28

0 Answers0