Website Summary Files Commits Branches @commit @diff @stats
path: root / Week11 / Exercise14.cpp
blob: 4b62d3a990ba7954b504f3d8607d5da7bd7164be (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
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>

char* getLine() {
	unsigned allocated = 8, size = 0;
	char* sentence = new char[allocated];

	char curr;
	std::cin.get(curr);
	while (curr != '\n') {
		sentence[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] = sentence[i];

			delete[] sentence;
			sentence = newSentenceArray;
		}
	}
	sentence[size] = '\0';

	return sentence;
}

int main() {
	char* first = getLine(), *second = getLine();

	bool foundCharacters[256] = { 0 };
	for (char* f = first; *f != '\0'; f++)
		foundCharacters[*f] = true;

	bool isAnagram = true;
	for (char* s = second; *s != '\0' && isAnagram; s++) {
		// Малките букви са отместени от главните с 32,
		// тоест всяка главна буква + 32 прави съответната малка буква
		// (и обратно).

		if ('A' <= *s && *s <= 'Z') {
			isAnagram = foundCharacters[*s] || foundCharacters[*s + 32];
		}
		else if ('a' <= *s && *s <= 'z') {
			isAnagram = foundCharacters[*s] || foundCharacters[*s - 32];
		}
	}

	if (isAnagram)
		std::cout << "Anagram" << std::endl;
	else
		std::cout << "Not an anagram" << std::endl;

	delete[] first;
	delete[] second;

	return 0;
}