From 65010da9ed11920b57e79ff6b197c0c64ceeba69 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 27 Feb 2024 17:33:06 +0200 Subject: [w1] Added exercises and solutions to ex1 and 2 --- week01/Exercise1.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 week01/Exercise1.cpp (limited to 'week01/Exercise1.cpp') diff --git a/week01/Exercise1.cpp b/week01/Exercise1.cpp new file mode 100644 index 0000000..5c4df9d --- /dev/null +++ b/week01/Exercise1.cpp @@ -0,0 +1,38 @@ +#include + +struct String { + char* stringPointer; + int stringSize; +}; + +String* concatString(const String& str1, const String& str2) { + String* biggerString = new String; + biggerString->stringSize = str1.stringSize + str2.stringSize; + biggerString->stringPointer = new char[biggerString->stringSize + 1]; // + 1 за да запазим и терминиращата нула + + for (int i = 0; i < str1.stringSize; i++) { + biggerString->stringPointer[i] = str1.stringPointer[i]; + } + + for (int i = 0; i < str2.stringSize; i++) { + biggerString->stringPointer[str1.stringSize + i] = str2.stringPointer[i]; + } + + biggerString->stringPointer[biggerString->stringSize] = '\0'; + return biggerString; +} + +int main() { + char str1Holder[] = "Hello"; + char str2Holder[] = "World"; + + String str1 = { str1Holder, 5 }; + String str2 = { str2Holder, 5 }; + + String* conc = concatString(str1, str2); + + std::cout << conc << std::endl; + + delete[] conc->stringPointer; + delete conc; +} -- cgit v1.2.3