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