Website Summary Files Commits Branches @commit @diff @stats
path: root / Week04 / Exercise03 / String.cpp
diff options
context:
space:
mode:
Commite5c06fbc864205a9736f07f9237ace80617ca471 (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-03-16 22:41:31 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-03-16 22:41:31 +0200
Filesf916efca600b104f3489a8501bffa448f11dfe79 /Week04/Exercise03/String.cpp
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.tar
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.tar.gz
oop-2025-solutions-e5c06fbc864205a9736f07f9237ace80617ca471.zip
Parent5624cd9b63bec13fc464a3d7820d703abd8de113 (diff)
feat(Week04): Add solutions
1 files changed, 81 insertions, 0 deletions
-rw-r--r--Week04/Exercise03/String.cpp81
diff --git a/Week04/Exercise03/String.cpp b/Week04/Exercise03/String.cpp
new file mode 100644
index 0000000..4fe49f4
--- /dev/null
+++ b/Week04/Exercise03/String.cpp
@@ -0,0 +1,81 @@
+#include "String.h"
+#include "string.h"
+
+void String::free() {
+ delete[] str;
+}
+void String::copyFrom(const String& other) {
+ length = other.length;
+
+ // Не забравяме за терминиращата нула!
+ str = new char[length + 1];
+ for (unsigned i = 0; i <= length; ++i)
+ str[i] = other.str[i];
+}
+
+String::String() {
+ length = 0;
+ str = new char[1];
+ str[0] = '\0';
+}
+
+String::~String() {
+ free();
+}
+
+String::String(const String& other) {
+ copyFrom(other);
+}
+
+String& String::operator=(const String& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+String::String(const char* str) {
+ length = strlen(str);
+
+ this->str = new char[length + 1];
+ for (unsigned i = 0; i <= length; ++i)
+ this->str[i] = str[i];
+}
+
+unsigned String::GetLength() {
+ return length;
+}
+
+const char* String::GetStr() {
+ return str;
+}
+
+String String::Substring(unsigned startIndex, unsigned endIndex) {
+ if (endIndex >= length)
+ endIndex = length - 1;
+
+ String ret;
+ // Не забравяме за терминиращата нула!
+ ret.length = endIndex - startIndex + 2;
+
+ ret.str = new char[ret.length];
+ for (unsigned i = 0; i <= ret.length; ++i)
+ ret.str[i] = this->str[i + startIndex];
+
+ return ret;
+}
+
+String String::Concat(const String& rightHalf) {
+ String ret;
+ // Не забравяме за терминиращата нула!
+ ret.length = length + rightHalf.length + 1;
+
+ ret.str = new char[ret.length];
+ for (unsigned i = 0; i < length; ++i)
+ ret.str[i] = this->str[i];
+ for (unsigned i = 0; i <= rightHalf.length; ++i)
+ ret.str[i + length] = this->str[i];
+
+ return ret;
+}