-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomString.cpp
More file actions
143 lines (123 loc) · 2.31 KB
/
CustomString.cpp
File metadata and controls
143 lines (123 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#define _CRT_SECURE_NO_WARNINGS
#include "CustomString.h"
#include <iostream>
#include <cstring>
#include <cassert>
#include <fstream>
#include <exception>
CustomString::CustomString(int capacity)
{
this->capacity = capacity;
this->array = new char[capacity];
this->size = 0;
}
CustomString::CustomString()
{
this->capacity = 10;
this->array = new char[capacity];
this->size = 0;
*(this->array + size++) = '\0';
}
CustomString::CustomString(const char* str) : CustomString()
{
setData(str);
}
CustomString::CustomString(const CustomString& other)
{
this->capacity = other.capacity;
array = new char[capacity];
this->size = other.size;
for(int i = 0; i < size; ++i)
{
*(array + i) = other.array[i];
}
}
CustomString::~CustomString()
{
delete[] array;
}
void CustomString::setData(const char* str)
{
if (str == nullptr)
{
str = "";
}
int strLength = strlen(str) + 1;
while (strLength > capacity)
{
expandArray();
}
strcpy(array, str);
size = strLength;
}
char* CustomString::readInput()
{
char ch;
while (std::cin.get(ch))
{
if (size == capacity)
{
expandArray();
}
array[size++] = ch;
}
if (capacity <= size)
{
expandArray();
}
array[size] = '\0';
return array;
}
char* CustomString::getArray()
{
return array;
}
char* CustomString::getArray() const
{
return array;
}
int CustomString::sizeOfArray() const
{
return this->size;
}
void CustomString::incrementSize()
{
size++;
}
int CustomString::capacityOfArray()
{
return this->capacity;
}
void CustomString::addElement(const char value)
{
if(size == capacity)
{
expandArray();
}
*(array + size) = value;
size++;
}
void CustomString::expandArray()
{
char* tempArray = new char[capacity * 2];
capacity = capacity * 2;
for (int i = 0; i < size; i++)
{
*(tempArray + i) = *(array + i);
}
delete[] array;
array = tempArray;
}
char CustomString::operator[] (int index) const
{
assert(index >= 0 && index < size);
return *(array + index);
}
CustomString& CustomString::operator=(const CustomString& other)
{
if (this != &other)
{
setData(other.array);
}
return *this;
}