blob: 83bc8f30496384842a4a06174d70168d3a5fc524 (
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>
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;
}
|