From 16620176eaf6ebf6e7e24a4e27f3e68f7431f2fc Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Mon, 17 Jan 2022 02:00:07 +0200 Subject: Add files via upload --- Sem. 15/task3.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Sem. 15/task3.cpp (limited to 'Sem. 15/task3.cpp') diff --git a/Sem. 15/task3.cpp b/Sem. 15/task3.cpp new file mode 100644 index 0000000..83bc8f3 --- /dev/null +++ b/Sem. 15/task3.cpp @@ -0,0 +1,48 @@ +#include + + +bool isLowerChar(char ch) +{ + return ch >= 'a' && ch <= 'z'; +} +char* sortLowerChars(const char* str) +{ + const int ALPHABET_SIZE = 26; + int hist[ALPHABET_SIZE] = { 0 }; + int newSize = 0; + + while (*str != '\0') + { + if (isLowerChar(*str)) + { + newSize++; + hist[*str - 'a']++; + } + + str++; + } + + char* result = new char[newSize + 1]; + result[newSize] = '\0'; + int resultIndex = 0; + + for (int i = 0; i < ALPHABET_SIZE; i++) + { + while (hist[i] > 0) + { + result[resultIndex++] = 'a' + i; + hist[i]--; + } + } + return result; + +} + +int main() +{ + char* result = sortLowerChars("bbzzAaaTYb"); + + std::cout << result << std::endl; + + delete[] result; +} \ No newline at end of file -- cgit v1.3.1-23-gd82d