-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.cpp
74 lines (52 loc) · 1.94 KB
/
task.cpp
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
#include "task.hpp"
// MARK: Удалить все строки короче 3 символов из массива строк
// MARK: Remove all strings shorter than 3 characters from the string array
custom_array findIndexs(char**& strings, int& array_length, int& str_length) {
struct custom_array array;
int ind_count = 0;
int* indexs = (int*) malloc(ind_count * sizeof(int));
for (int i = 0; i < array_length; i++) {
int count = 0;
for (int j = 0; strings[i][j] != '\0'; j++) { count += 1; }
if (count < 3) {
ind_count += 1;
indexs = (int*) realloc(indexs, ind_count * sizeof(int));
indexs[ind_count - 1] = i;
}
}
array.ptr = indexs;
array.length = ind_count;
return array;
}
void deleteElemet(char**& strings, int& array_length, int index) {
free(strings[index]);
for (int i = index; i < array_length - 1; i++) {
strings[i] = strings[i + 1];
}
array_length -= 1;
strings = (char**) realloc(strings, array_length * sizeof(char*));
}
int main() {
int str_length = 10;
int array_length = 5;
char** strings = (char**) malloc(array_length * sizeof(char*));
for (int i = 0; i < array_length; i++) {
strings[i] = (char*) malloc(str_length * sizeof(char));
scanf("%s", strings[i]);
}
print2DArray(strings, array_length, str_length);
// ptr_array, length
struct custom_array ind_array = findIndexs(strings, array_length, str_length);
for (int i = 0; i < ind_array.length; i++) {
deleteElemet(strings, array_length, ind_array.ptr[i] - i);
}
print2DArray(strings, array_length, str_length);
for (int i = 0; i < array_length; i++) {
free(strings[i]);
strings[i] = nullptr;
}
free(strings);
free(ind_array.ptr);
strings = nullptr;
ind_array.ptr = nullptr;
}