How to store a dynamic C string

For an assignment in my college data structures class, we have to define functions for a palindrome class that takes a word, checks if it is a palindrome, stores it as half the word, and can access certain aspects of it in C++.

I need to declare a string variable in the palindrome class with a dynamic size so that I can store words of different sizes.

I should note that all of this must be done from scratch, and I’m not allowed to use the string or cstring classes.

Is this possible? Or do I need a different approach?

Header file for the class:

class Palindrome{

   public:

      Palindrome();
      Palindrome(const char* str);
      Palindrome(const Palindrome& other);
      ~Palindrome();
      Palindrome& operator=(const Palindrome& other);

      void set_palindrome(const char* str);

      int length() const;
      int storage_size() const;
      char char_at(int pos) const;
      void set_char_at(char c, int pos);

      int compare(const Palindrome& other) const;
      int compare(const char* other) const;
      bool operator<(const Palindrome& other) const;
      bool operator<(const char* other) const;

      static bool isPalindrome(const char* str);

      friend std::ostream& operator<<(std::ostream& out, const Palindrome& pal);
    private:
        int len;
        int store_size;
        
};

I tried to initialize a C string as a private data member, but it required me to specify a size for it. I’m not sure how to change the size of a C string, or if I just have to make a new one.

14

Given the constraints you’ve been given, your best options would be to use a std::unique_ptr<char[]>.

While something like std::vector<char> would handle resizing for you, it isn’t so nice if you have to maintain null termination of the string, and in this case, it doesn’t appear you have to append or resize the string.

So, what I would recommend is using a std::unique_ptr<char[]>, which you can allocate with std::make_unique<char[]>(length), and then do a string copy via a handful of methods. The unique_ptr will handle deallocation of the memory. The main thing you have to worry about is null termination, if that’s needed.

1

I disagree with the other answers.

The purpose of your assignment is to write a class that properly encapsulates new[] and delete[] for an array of dynamically-allocated data.

Make sure to obey the Rule of Three/Five/Zero.

To answer the the question-in-a-question: yes, to resize your string, you must:

  • allocate a new array and, if successful,
  • copy the content of the old array to the new one,
  • add the new content (if the array is increased in size),
  • delete the old array,
  • update the object to reference the new array

2

You don’t need to use string, you could use std::vector for storage.

It looks like the string is being provided as a C-style string (e.g const char*). So in the constructor check if it’s a palindrome, if it’s not then throw an exception (this is the only way to “fail” construction).

If it is a palindrome you can store it in a std::vector or std::list.

If your professor really doesn’t want you using standard library then you’re going to have to use new.. but at this point you’re intentionally writing less-safe code (standard library containers handle the lifetime management of memory for you while using new and delete means you are more prone to memory leaks)

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật