Website Summary Files Commits Branches @commit @diff @stats
path: root / Week05 / Exercise02 / String.cpp
diff options
context:
space:
mode:
Commit571dc57f812a685bda7bd3baf8aaf2ef56feae65 (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-03-28 16:19:58 +0200
Committer
 Syndamia <kamen@syndamia.com>
2026-03-28 16:19:58 +0200
Filesd69f560eccb705339f44be22369bb8fed025e642 /Week05/Exercise02/String.cpp
oop-2025-solutions-571dc57f812a685bda7bd3baf8aaf2ef56feae65.tar
oop-2025-solutions-571dc57f812a685bda7bd3baf8aaf2ef56feae65.tar.gz
oop-2025-solutions-571dc57f812a685bda7bd3baf8aaf2ef56feae65.zip
Parente5c06fbc864205a9736f07f9237ace80617ca471 (diff)
feat(Week05): Add solutions
1 files changed, 103 insertions, 0 deletions
-rw-r--r--Week05/Exercise02/String.cpp103
diff --git a/Week05/Exercise02/String.cpp b/Week05/Exercise02/String.cpp
new file mode 100644
index 0000000..e2b082d
--- /dev/null
+++ b/Week05/Exercise02/String.cpp
@@ -0,0 +1,103 @@
+#include "String.h"
+#include <cstring>
+
+void String::free() {
+ delete[] str;
+}
+
+void String::copyFrom(const String& other) {
+ length = other.length;
+
+ // Не забравяме за терминиращата нула
+ str = new char[length + 1];
+ strncpy(str, other.str, length + 1);
+}
+
+String::String() {
+ str = nullptr;
+ length = 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(String&& other) {
+ str = other.str;
+ length = other.length;
+
+ other.str = nullptr;
+ other.length = 0;
+}
+
+String& String::operator=(String&& other) {
+ if (this != &other) {
+ free();
+
+ str = other.str;
+ length = other.length;
+
+ other.str = nullptr;
+ other.length = 0;
+ }
+ return *this;
+}
+
+String::String(const char* str) {
+ length = strlen(str);
+ this->str = new char[length + 1];
+ strncpy(this->str, str, length + 1);
+}
+
+String String::Substring(unsigned startIndex, unsigned length) const {
+ String result;
+ if (startIndex >= this->length) {
+ return result;
+ }
+
+ if (startIndex + length >= this->length) {
+ length = this->length - startIndex;
+ }
+
+ result.length = length;
+ result.str = new char[length + 1];
+ strncpy(result.str, str, length);
+ result.str[length + 1] = '\0';
+
+ return result;
+}
+
+void String::Concat(const String& end) {
+ char* newStr = new char[length + end.length + 1];
+ strncpy(newStr, str, length);
+ strncpy(newStr + length, end.str, end.length + 1);
+
+ delete[] str;
+ str = newStr;
+ length += end.length;
+}
+
+unsigned String::CountWords() const {
+ unsigned words = 0;
+ bool prevBlank = true;
+
+ for (const char* s = str; *s != '\0'; ++s) {
+ if (prevBlank && *s != ' ')
+ ++words;
+ prevBlank = *s == ' ';
+ }
+
+ return words;
+}