35657

Untitled

May 26th, 2023 (edited)
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class String {
  10.  
  11. public:
  12.  
  13.     String() {
  14.         size = 0;
  15.         capacity = 15;
  16.         str = new char[15];
  17.     }
  18.  
  19.     String(const int string_capacity) {
  20.         size = 0;
  21.         capacity = string_capacity;
  22.         str = new char[capacity];
  23.     }
  24.  
  25.     String(const char new_str[]) {
  26.         capacity = strlen(new_str) + 1;
  27.         str = new char[capacity];
  28.         strcpy(str, new_str);
  29.         size = capacity - 1;
  30.     }
  31.  
  32.     void SetString(const char new_str[]) {
  33.         int new_space = strlen(new_str) + 1;
  34.         if (capacity < new_space) {
  35.             delete[] str;
  36.             capacity = new_space;
  37.             str = new char[capacity];
  38.         }
  39.         strcpy(str, new_str);
  40.         size = capacity - 1;
  41.     }
  42.  
  43.     const char* GetString() {
  44.         return str;
  45.     }
  46.  
  47.     void SetChar(const int index, const char ch) {
  48.         if (index < 0 || index >= size) {
  49.             cout << "Invalid index" << endl;
  50.             return;
  51.         }
  52.         str[index] = ch;
  53.     }
  54.  
  55.     char GetChar(const int index) {
  56.         if (index < 0 || index >= size) {
  57.             cout << "Invalid index" << endl;
  58.             abort;
  59.         }
  60.         return str[index];
  61.     }
  62.  
  63.     int Size() {
  64.         return size;
  65.     }
  66.  
  67.     int Capacity() {
  68.         return capacity;
  69.     }
  70.  
  71.     ~String() {
  72.         delete[] str;
  73.     }
  74.  
  75. private:
  76.     int size;
  77.     int capacity;
  78.     char* str;
  79. };
  80.  
  81.  
  82. int main() {
  83.     String my_string;
  84.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  85.     my_string.SetString("Hello");
  86.     cout << my_string.GetString() << endl;
  87.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  88.     my_string.SetString("Hello my dear friend");
  89.     cout << my_string.GetString() << endl;
  90.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  91.     my_string.SetChar(2, 'm');
  92.     cout << my_string.GetString() << endl;
  93.     cout << my_string.GetChar(1) << endl;
  94.     String my_new_string("1234");
  95.     cout << my_new_string.GetString() << endl;
  96.     cout << "Size: " << my_new_string.Size() << " Capacity: " << my_new_string.Capacity() << endl;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment