Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- //mystring class stores the string in the character pointer
- class mystring {
- // attributes of class
- int mLength;
- char *mData;
- public:
- mystring();
- mystring(const char cstring[]);
- mystring(const mystring &rhs);
- ~mystring();
- void append(char c);
- void append(const mystring &rhs);
- void pop();
- void clear();
- int length() const;
- char at(int index) const;
- const char *c_str() const;
- };
- // default constructor of class
- mystring::mystring() {
- // set length to 0
- mLength = 0;
- // set data to null pointer
- mData = NULL;
- }
- // parameterized constructor of class with character array
- mystring::mystring(const char cstring[]) {
- // set length to 0
- mLength = 0;
- // determine the length of cstring
- while (cstring[mLength] != '\0') {
- mLength++;
- }
- // dynamically allocate character array of length mLength + 1
- mData = new char[mLength + 1];
- // copy data from cstring to mData
- for (int i = 0; i < mLength; i++) {
- mData[i] = cstring[i];
- }
- // store null pointer at mLength index
- mData[mLength] = '\0';
- }
- // copy constructor of class
- mystring::mystring(const mystring &rhs) {
- // copy length of rhs to mLength
- mLength = rhs.mLength;
- // dynamically allocate memory
- mData = new char[mLength + 1];
- // copy data of rhs to mData
- for (int i = 0; i < mLength + 1; i++) {
- mData[i] = rhs.mData[i];
- }
- }
- // destructor of class
- mystring::~mystring() {
- // deallocate memory if any exist
- if (mData != NULL)
- delete[] mData;
- // set mLength to 0
- mLength = 0;
- }
- // This function is used to append character to the character array
- void mystring::append(char c) {
- // copy mData to temp
- char *temp = mData;
- // create a new memeory with increased length
- mData = new char[mLength + 2];
- // copy each character in temp to mData
- for (int i = 0; i < mLength; i++) {
- mData[i] = temp[i];
- }
- // add character to end and add null character
- mData[mLength] = c;
- mData[mLength + 1] = '\0';
- // increment length of string by 1
- mLength += 1;
- // deallocate old memory
- if (temp != NULL)
- delete[] temp;
- }
- // This function is used append data in another mystring to this string
- void mystring::append(const mystring &rhs) {
- // copy mData to temp
- char *temp = mData;
- // create a new memeory with increased length
- mData = new char[mLength + rhs.length() + 1];
- // copy each character in temp to mData
- for (int i = 0; i < mLength; i++) {
- mData[i] = temp[i];
- }
- // copy each character in rhs data to mData
- for (int i = mLength; i<(mLength + rhs.length()); i++) {
- mData[i] = rhs.at(i - mLength);
- }
- // add null character at end
- mData[mLength + rhs.length()] = '\0';
- // increment mLength by rhs's mLength
- mLength += rhs.length();
- // deallocate old memory
- if (temp != NULL)
- delete[] temp;
- }
- // This function is used to remove last character from data
- void mystring::pop() {
- // copy mData to temp
- char *temp = mData;
- // create a new memeory with decreased length
- mData = new char[1];
- // add null character at end
- mData[0] = '\0';
- // set length to 0
- mLength = 0;
- // deallocate old memory
- if (temp != NULL)
- delete[] temp;
- }
- // This function is used to clear the data in mystring
- void mystring::clear() {
- // deallocate memory if any exist
- if (mData != NULL) {
- delete[] mData;
- mData = NULL;
- }
- // set mLength to 0
- mLength = 0;
- }
- // This function is used to return length of string
- int mystring::length() const {
- return mLength;
- }
- // This function is used to return character at specific index
- char mystring::at(int index) const {
- // return null if index is out of range
- if (index<0 || index >= mLength)
- return '\0';
- // return character at index if in range
- return mData[index];
- }
- // This function is used to return data
- const char *mystring::c_str() const {
- return mData;
- }
- int main() {
- cout << "Creating mystring1 with empty constructor" << endl;
- mystring mystring1;
- cout << "Length of mystring1 is " << mystring1.length() << endl;
- cout << "Accessing character at 2 of mystring1 returns " << mystring1.at(2) << endl;
- cout << "Appending character A to mystring1" << endl;
- mystring1.append('A');
- cout << "Length of mystring1 is " << mystring1.length() << endl;
- cout << "Data of mystring1 is " << mystring1.c_str() << endl;
- cout << "Accessing character at 0 of mystring1 returns " << mystring1.at(0) << endl;
- cout << "Accessing character at 2 of mystring1 returns " << mystring1.at(2) << endl;
- cout << "\nCreating mystring2 with character array" << endl;
- char data[] = "Hello";
- mystring mystring2(data);
- cout << "Length of mystring2 is " << mystring2.length() << endl;
- cout << "Data of mystring2 is " << mystring2.c_str() << endl;
- cout << "Accessing character at 2 of mystring2 returns " << mystring2.at(2) << endl;
- cout << "Appending character ! to mystring2" << endl;
- mystring2.append('!');
- cout << "Length of mystring2 is " << mystring2.length() << endl;
- cout << "Data of mystring2 is " << mystring2.c_str() << endl;
- cout << "Accessing character at 5 of mystring2 returns " << mystring2.at(5) << endl;
- cout << "Popping character from mystring2" << endl;
- mystring2.pop();
- cout << "Length of mystring2 is " << mystring2.length() << endl;
- cout << "Data of mystring2 is " << mystring2.c_str() << endl;
- cout << "Appending mystring1 to mystring2" << endl;
- mystring2.append(mystring1);
- cout << "Length of mystring2 is " << mystring2.length() << endl;
- cout << "Data of mystring2 is " << mystring2.c_str() << endl;
- cout << "\nCreating mystring3 with mystring2" << endl;
- mystring mystring3(mystring2);
- cout << "Length of mystring3 is " << mystring3.length() << endl;
- cout << "Data of mystring3 is " << mystring3.c_str() << endl;
- cout << "Appending string 'll!' to mystring3" << endl;
- mystring3.append("ll!");
- cout << "Length of mystring3 is " << mystring3.length() << endl;
- cout << "Data of mystring3 is " << mystring3.c_str() << endl;
- cout << "Clearing character from mystring3" << endl;
- mystring3.clear();
- cout << "Length of mystring3 is " << mystring3.length() << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement