Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. //mystring class stores the string in the character pointer
  7. class mystring {
  8. // attributes of class
  9. int mLength;
  10. char *mData;
  11.  
  12. public:
  13. mystring();
  14. mystring(const char cstring[]);
  15. mystring(const mystring &rhs);
  16. ~mystring();
  17. void append(char c);
  18. void append(const mystring &rhs);
  19. void pop();
  20. void clear();
  21. int length() const;
  22. char at(int index) const;
  23. const char *c_str() const;
  24. };
  25.  
  26. // default constructor of class
  27. mystring::mystring() {
  28. // set length to 0
  29. mLength = 0;
  30.  
  31. // set data to null pointer
  32. mData = NULL;
  33. }
  34.  
  35. // parameterized constructor of class with character array
  36. mystring::mystring(const char cstring[]) {
  37. // set length to 0
  38. mLength = 0;
  39.  
  40. // determine the length of cstring
  41. while (cstring[mLength] != '\0') {
  42. mLength++;
  43. }
  44.  
  45. // dynamically allocate character array of length mLength + 1
  46. mData = new char[mLength + 1];
  47.  
  48. // copy data from cstring to mData
  49. for (int i = 0; i < mLength; i++) {
  50. mData[i] = cstring[i];
  51. }
  52. // store null pointer at mLength index
  53. mData[mLength] = '\0';
  54. }
  55.  
  56. // copy constructor of class
  57. mystring::mystring(const mystring &rhs) {
  58. // copy length of rhs to mLength
  59. mLength = rhs.mLength;
  60.  
  61. // dynamically allocate memory
  62. mData = new char[mLength + 1];
  63.  
  64. // copy data of rhs to mData
  65. for (int i = 0; i < mLength + 1; i++) {
  66. mData[i] = rhs.mData[i];
  67. }
  68. }
  69.  
  70. // destructor of class
  71. mystring::~mystring() {
  72. // deallocate memory if any exist
  73. if (mData != NULL)
  74. delete[] mData;
  75. // set mLength to 0
  76. mLength = 0;
  77. }
  78.  
  79. // This function is used to append character to the character array
  80. void mystring::append(char c) {
  81. // copy mData to temp
  82. char *temp = mData;
  83.  
  84. // create a new memeory with increased length
  85. mData = new char[mLength + 2];
  86. // copy each character in temp to mData
  87. for (int i = 0; i < mLength; i++) {
  88. mData[i] = temp[i];
  89. }
  90. // add character to end and add null character
  91. mData[mLength] = c;
  92. mData[mLength + 1] = '\0';
  93.  
  94. // increment length of string by 1
  95. mLength += 1;
  96.  
  97. // deallocate old memory
  98. if (temp != NULL)
  99. delete[] temp;
  100.  
  101. }
  102.  
  103. // This function is used append data in another mystring to this string
  104. void mystring::append(const mystring &rhs) {
  105. // copy mData to temp
  106. char *temp = mData;
  107.  
  108. // create a new memeory with increased length
  109. mData = new char[mLength + rhs.length() + 1];
  110.  
  111. // copy each character in temp to mData
  112. for (int i = 0; i < mLength; i++) {
  113. mData[i] = temp[i];
  114. }
  115.  
  116. // copy each character in rhs data to mData
  117. for (int i = mLength; i<(mLength + rhs.length()); i++) {
  118. mData[i] = rhs.at(i - mLength);
  119. }
  120. // add null character at end
  121. mData[mLength + rhs.length()] = '\0';
  122.  
  123. // increment mLength by rhs's mLength
  124. mLength += rhs.length();
  125.  
  126. // deallocate old memory
  127. if (temp != NULL)
  128. delete[] temp;
  129. }
  130.  
  131. // This function is used to remove last character from data
  132. void mystring::pop() {
  133. // copy mData to temp
  134. char *temp = mData;
  135.  
  136. // create a new memeory with decreased length
  137. mData = new char[1];
  138.  
  139. // add null character at end
  140. mData[0] = '\0';
  141.  
  142. // set length to 0
  143. mLength = 0;
  144.  
  145. // deallocate old memory
  146. if (temp != NULL)
  147. delete[] temp;
  148. }
  149.  
  150. // This function is used to clear the data in mystring
  151. void mystring::clear() {
  152. // deallocate memory if any exist
  153. if (mData != NULL) {
  154. delete[] mData;
  155. mData = NULL;
  156. }
  157. // set mLength to 0
  158. mLength = 0;
  159. }
  160.  
  161. // This function is used to return length of string
  162. int mystring::length() const {
  163. return mLength;
  164. }
  165.  
  166. // This function is used to return character at specific index
  167. char mystring::at(int index) const {
  168. // return null if index is out of range
  169. if (index<0 || index >= mLength)
  170. return '\0';
  171. // return character at index if in range
  172. return mData[index];
  173. }
  174.  
  175. // This function is used to return data
  176. const char *mystring::c_str() const {
  177. return mData;
  178. }
  179.  
  180. int main() {
  181. cout << "Creating mystring1 with empty constructor" << endl;
  182. mystring mystring1;
  183. cout << "Length of mystring1 is " << mystring1.length() << endl;
  184. cout << "Accessing character at 2 of mystring1 returns " << mystring1.at(2) << endl;
  185. cout << "Appending character A to mystring1" << endl;
  186. mystring1.append('A');
  187. cout << "Length of mystring1 is " << mystring1.length() << endl;
  188. cout << "Data of mystring1 is " << mystring1.c_str() << endl;
  189. cout << "Accessing character at 0 of mystring1 returns " << mystring1.at(0) << endl;
  190. cout << "Accessing character at 2 of mystring1 returns " << mystring1.at(2) << endl;
  191.  
  192.  
  193. cout << "\nCreating mystring2 with character array" << endl;
  194. char data[] = "Hello";
  195. mystring mystring2(data);
  196. cout << "Length of mystring2 is " << mystring2.length() << endl;
  197. cout << "Data of mystring2 is " << mystring2.c_str() << endl;
  198. cout << "Accessing character at 2 of mystring2 returns " << mystring2.at(2) << endl;
  199. cout << "Appending character ! to mystring2" << endl;
  200. mystring2.append('!');
  201. cout << "Length of mystring2 is " << mystring2.length() << endl;
  202. cout << "Data of mystring2 is " << mystring2.c_str() << endl;
  203. cout << "Accessing character at 5 of mystring2 returns " << mystring2.at(5) << endl;
  204. cout << "Popping character from mystring2" << endl;
  205. mystring2.pop();
  206. cout << "Length of mystring2 is " << mystring2.length() << endl;
  207. cout << "Data of mystring2 is " << mystring2.c_str() << endl;
  208. cout << "Appending mystring1 to mystring2" << endl;
  209. mystring2.append(mystring1);
  210. cout << "Length of mystring2 is " << mystring2.length() << endl;
  211. cout << "Data of mystring2 is " << mystring2.c_str() << endl;
  212.  
  213. cout << "\nCreating mystring3 with mystring2" << endl;
  214. mystring mystring3(mystring2);
  215. cout << "Length of mystring3 is " << mystring3.length() << endl;
  216. cout << "Data of mystring3 is " << mystring3.c_str() << endl;
  217. cout << "Appending string 'll!' to mystring3" << endl;
  218. mystring3.append("ll!");
  219. cout << "Length of mystring3 is " << mystring3.length() << endl;
  220. cout << "Data of mystring3 is " << mystring3.c_str() << endl;
  221. cout << "Clearing character from mystring3" << endl;
  222. mystring3.clear();
  223. cout << "Length of mystring3 is " << mystring3.length() << endl;
  224.  
  225. system("pause");
  226.  
  227. return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement