Website Summary Files Commits Branches @commit @diff @stats
path: root / Week11 / Exercise07.cpp
blob: e434919544be2d675f8306224af64f9ababb76c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

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;
}