Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit65658b8f009926b16d920a24694e0559a716a7cc (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-12-01 17:31:08 -0500
Committer
 GitHub <noreply@github.com>
2021-12-01 17:31:08 -0500
Files7f25172363fc7acfbcbe77b084cf144ea28c4a6d
Introduction_to_programming_FMI-65658b8f009926b16d920a24694e0559a716a7cc.tar
Introduction_to_programming_FMI-65658b8f009926b16d920a24694e0559a716a7cc.tar.gz
Introduction_to_programming_FMI-65658b8f009926b16d920a24694e0559a716a7cc.zip
Parentd00ad8def7f62050ec8a0cc9a7b2fd23bff0546e (diff)
Create task1_with_extra_memory.cpp
1 files changed, 64 insertions, 0 deletions
-rw-r--r--Sem. 08/Solutions/task1_with_extra_memory.cpp64
diff --git a/Sem. 08/Solutions/task1_with_extra_memory.cpp b/Sem. 08/Solutions/task1_with_extra_memory.cpp
new file mode 100644
index 0000000..036a564
--- /dev/null
+++ b/Sem. 08/Solutions/task1_with_extra_memory.cpp
@@ -0,0 +1,64 @@
+#include <iostream>
+#include <cstring>
+
+#pragma warning (disable:4996)
+
+using namespace std;
+
+const unsigned CAPACITY = 1024;
+
+bool isPrefix(const char* text, const char* prefix)
+{
+ int i = 0;
+ while (prefix[i] != '\0')
+ {
+ if (text[i] == '\0' || text[i] != prefix[i])
+ return false;
+ i++;
+ }
+ return true;
+}
+
+//допускаме, че в text има достатъчно място.
+void stringReplace(char* text, const char* pattern, char* replace)
+{
+ char newText[CAPACITY]; //extra space
+ unsigned newTextIter = 0;
+
+ char* textIter = text;
+
+ unsigned replacedSize = strlen(replace);
+ unsigned patternSize = strlen(pattern);
+
+ while (*textIter != '\0')
+ {
+ if (isPrefix(textIter, pattern))
+ {
+ newText[newTextIter] = '\0'; //so strcat would work
+ strcat(newText, replace);
+
+ newTextIter += replacedSize;
+ textIter += patternSize;
+ }
+ else
+ {
+ newText[newTextIter++] = *textIter;
+ textIter++;
+ }
+
+ }
+ newText[newTextIter] = '\0';
+ strcpy(text, newText);
+}
+
+
+int main()
+{
+ char text[CAPACITY] = "I am the best of the best!";
+ char pattern[] = "best";
+ char replace[] = "worst";
+
+ stringReplace(text, pattern, replace);
+
+ cout << text << endl;
+}