Website Summary Files Commits Branches @commit @diff @stats
path: root / Week11 / Exercise11.cpp
diff options
context:
space:
mode:
Commit6bb7b68394ebebbc69e76bee59e9351386115453 (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-12-14 21:56:30 +0200
Committer
 Syndamia <kamen@syndamia.com>
2025-12-14 21:56:30 +0200
Filesc31eb47780b5bfe4d038b6d6b8f32a385d717b96 /Week11/Exercise11.cpp
upp-2025-solutions-6bb7b68394ebebbc69e76bee59e9351386115453.tar
upp-2025-solutions-6bb7b68394ebebbc69e76bee59e9351386115453.tar.gz
upp-2025-solutions-6bb7b68394ebebbc69e76bee59e9351386115453.zip
Parent5ce214686e668bf185222b518b78d7f47a20b4fd (diff)
feat(Week11): Add solutions
1 files changed, 48 insertions, 0 deletions
-rw-r--r--Week11/Exercise11.cpp48
diff --git a/Week11/Exercise11.cpp b/Week11/Exercise11.cpp
new file mode 100644
index 0000000..988bea6
--- /dev/null
+++ b/Week11/Exercise11.cpp
@@ -0,0 +1,48 @@
+#include <iostream>
+
+bool streq(const char* str1, const char* str2) {
+ while (*str1 != '\0' && *str2 != '\0') {
+ if (*str1++ != *str2++)
+ return false;
+ }
+ return true;
+}
+
+unsigned strlen(const char* str) {
+ unsigned len = 0;
+ while (*str++ != '\0')
+ len++;
+ return len;
+}
+
+int main() {
+ const unsigned MAX_LINE_LENGTH = 1024;
+ char first[MAX_LINE_LENGTH], second[MAX_LINE_LENGTH];
+
+ std::cin.getline(first, MAX_LINE_LENGTH);
+ std::cin.getline(second, MAX_LINE_LENGTH);
+
+ // Как премахваме букви? Най-лесния начин е просто да ги презапишем.
+ // Все едно отместваме частта от низа след буквата, така че да се
+ // застъпи с това, което премахваме.
+ //
+ // Например, ако в "abcde" искаме да премахнем c, тогава просто
+ // на позицията на c ще запишем d, на позицията на d ще запишем е
+ // и на позицията на e ще запишем терминираща нула.
+
+ unsigned secondLen = strlen(second);
+
+ for (unsigned readIndex = 0, writeIndex = 0; first[readIndex] != '\0'; readIndex++, writeIndex++) {
+ if (streq(first + readIndex, second))
+ readIndex += secondLen;
+
+ if (readIndex != writeIndex) {
+ first[writeIndex] = first[readIndex];
+ first[writeIndex + 1] = '\0';
+ }
+ }
+
+ std::cout << first << std::endl;
+
+ return 0;
+}