Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. class MyString {
  7. char * strPtr;
  8. unsigned int strSize, strCapacity;
  9. public:
  10. MyString() {
  11. clear();
  12. }
  13.  
  14. MyString(const char * string) {
  15. strCapacity = strlen(string);
  16. cout << "String size is " << strCapacity << endl;
  17. strSize = strCapacity;
  18. strPtr = new char(strCapacity + 1);
  19. strcpy(strPtr, string);
  20. }
  21.  
  22. MyString(MyString&a){
  23. strCapacity = a.strCapacity;
  24. strSize = strCapacity;
  25. strPtr = new char(a.strCapacity + 1);
  26. strcpy(strPtr, a.strPtr);
  27. }
  28.  
  29. ~MyString(){
  30. delete[] strPtr;
  31. }
  32.  
  33. void reserve (unsigned int c){
  34. if (strCapacity < c){
  35. char * cpy = new char(strCapacity + 1);
  36. strcpy(cpy, strPtr);
  37. strCapacity = c;
  38. strPtr = new char(strCapacity + 1);
  39. strcpy(strPtr, cpy);}
  40. }
  41.  
  42. MyString & append(MyString & str) {
  43. strSize = strSize + str.strSize;
  44. reserve(str.strCapacity + strCapacity + 1);
  45. strcat(strPtr, str.strPtr);
  46. }
  47.  
  48. MyString & assign(MyString & str) {
  49. reserve(str.strCapacity);
  50. strcpy(strPtr, str.strPtr);
  51.  
  52. }
  53.  
  54. const char * c_str() {
  55. return strPtr;
  56. }
  57.  
  58. unsigned int size(){
  59. return strSize;
  60. }
  61.  
  62. unsigned int capacity(){
  63. return strCapacity;
  64. }
  65.  
  66. void clear(){
  67. strPtr = new char(1);
  68. strPtr[0] = '\0';
  69. }
  70.  
  71. bool empty(){
  72. if (strPtr[0] == '\0')
  73. return true;
  74. else
  75. return false;
  76. }
  77.  
  78. };
  79.  
  80.  
  81. int main(){
  82. MyString s1;
  83. MyString s2("I hate my fucking life please work");
  84. cout << s1.c_str() << endl;
  85. cout << s2.c_str() << endl;
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement