Website Summary Files Commits Branches @commit @diff @stats
path: root / Week05 / Exercise02 / Text.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/Text.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, 67 insertions, 0 deletions
-rw-r--r--Week05/Exercise02/Text.cpp67
diff --git a/Week05/Exercise02/Text.cpp b/Week05/Exercise02/Text.cpp
new file mode 100644
index 0000000..b6143c0
--- /dev/null
+++ b/Week05/Exercise02/Text.cpp
@@ -0,0 +1,67 @@
+#include "Text.h"
+
+void Text::free() {
+ delete[] lines;
+}
+
+void Text::copyFrom(const Text& other) {
+ lineCount = other.lineCount;
+
+ lines = new String[lineCount];
+ for (unsigned i = 0; i < lineCount; ++i)
+ lines[i] = other.lines[i];
+}
+
+Text::Text() {
+ lines = nullptr;
+ lineCount = 0;
+}
+
+Text::~Text() {
+ free();
+}
+
+Text::Text(const Text& other) {
+ copyFrom(other);
+}
+
+Text& Text::operator=(const Text& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+Text::Text(Text&& other) {
+ lines = other.lines;
+ lineCount = other.lineCount;
+
+ other.lines = nullptr;
+ other.lineCount = 0;
+}
+
+unsigned Text::LineCount() const {
+ return lineCount;
+}
+
+unsigned Text::WordCount() const {
+ unsigned totalCount = 0;
+
+ for (unsigned i = 0; i < lineCount; ++i)
+ totalCount += lines[i].CountWords();
+
+ return totalCount;
+}
+
+Text Text::SubText(unsigned startLine, unsigned startCol, unsigned endLine, unsigned endCol) const {
+ Text result;
+ result.lineCount = endLine - startLine + 1;
+ result.lines = new String[lineCount];
+
+ for (unsigned i = 0; i < result.lineCount; ++i) {
+ result.lines[i] = lines[i + startLine].Substring(startCol, endCol - startCol + 1);
+ }
+
+ return result;
+}