diff options
Diffstat (limited to 'week01/Exercise1.cpp')
| -rw-r--r-- | week01/Exercise1.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
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 <iostream> + +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; +} |
