From 6bb7b68394ebebbc69e76bee59e9351386115453 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 14 Dec 2025 21:56:30 +0200 Subject: feat(Week11): Add solutions --- Week11/Exercise07.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Week11/Exercise07.cpp (limited to 'Week11/Exercise07.cpp') diff --git a/Week11/Exercise07.cpp b/Week11/Exercise07.cpp new file mode 100644 index 0000000..e434919 --- /dev/null +++ b/Week11/Exercise07.cpp @@ -0,0 +1,48 @@ +#include + +int main() { + const unsigned PATTERN_SIZE = 5; + char pattern[PATTERN_SIZE + 1]; + std::cin.getline(pattern, PATTERN_SIZE+1); + + unsigned allocated = 8, size = 0; + char* text = new char[allocated]; + + char curr; + std::cin.get(curr); + while (curr != '\n') { + text[size++] = curr; + std::cin.get(curr); + + // Реоразмеряване + if (size >= allocated) { + allocated *= 2; + char* newSentenceArray = new char[allocated]; + + for (unsigned i = 0; i < size; i++) + newSentenceArray[i] = text[i]; + + delete[] text; + text = newSentenceArray; + } + } + text[size] = '\0'; + + unsigned matches = 0; + for (unsigned i = 0; i < size - PATTERN_SIZE + 1; i++) { + bool allMatch = true; + + // Проверяваме дали от текущата буква в текста, нататък, + // низа съвпада с шаблона. + for (unsigned j = 0; j < PATTERN_SIZE; j++) + allMatch = allMatch && (text[i + j] == pattern[j]); + + matches += allMatch; + } + + std::cout << matches << std::endl; + + delete[] text; + + return 0; +} -- cgit v1.3.1-23-gd82d