-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathchar_changer.cpp
More file actions
111 lines (103 loc) · 2.84 KB
/
char_changer.cpp
File metadata and controls
111 lines (103 loc) · 2.84 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <cstddef>
#include <stdexcept>
#include <cctype>
size_t CharChanger(char array[], size_t size, char delimiter = ' ')
{
// throw std::runtime_error{"Not implemented"};
size_t j = 0;
size_t i = 0;
while (i < size && array[i] != '\0')
{
if (array[i] == ' ')
{
// Пропускаем все пробелы
size_t spaces = 0;
while (i < size && array[i] == ' ')
{
i++;
spaces++;
}
// Записываем разделитель если есть пробелы
if (spaces > 0)
{
if (j < size - 1)
{
array[j] = delimiter;
j++;
}
else
{
break;
}
}
}
else
{
// Определяем длину последовательности одинаковых символов
char original = array[i];
size_t count = 0;
while (i + count < size && array[i + count] == original && array[i + count] != '\0')
{
count++;
}
// Преобразуем символ
char current = original;
if (std::isdigit(static_cast<unsigned char>(original)))
{
current = '*';
}
else if (std::islower(static_cast<unsigned char>(original)))
{
current = std::toupper(static_cast<unsigned char>(original));
}
else if (!std::isupper(static_cast<unsigned char>(original)))
{
current = '_';
}
// Записываем результат с учетом правила повторений
if (count == 1)
{
if (j < size - 1)
{
array[j] = current;
j++;
}
else
{
break;
}
}
else
{
if (j + 1 < size - 1)
{
array[j] = current;
if (count >= 10)
{
array[j + 1] = '0';
}
else
{
array[j + 1] = '0' + count;
}
j += 2;
}
else
{
break;
}
}
i += count;
}
}
// Завершаем строку
if (j < size)
{
array[j] = '\0';
}
else if (size > 0)
{
array[size - 1] = '\0';
}
return j;
}