-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2.1.c
268 lines (234 loc) · 5.43 KB
/
m2.1.c
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef enum {
fsc_ok,
fsc_inv_num_arg,
fsc_inv_flag,
fsc_un_flag,
fsc_inv_num,
fsc_mem_er
} function_status_code;
function_status_code flag_compare(int argc, char* argv[]);
function_status_code l_fl(int argc, char* argv[]);
function_status_code r_fl(int argc, char* argv[]);
function_status_code u_fl(int argc, char* argv[]);
function_status_code n_fl(int argc, char* argv[]);
function_status_code c_fl(int argc, char* argv[]);
unsigned long long str_length(const char* str) {
if (str == NULL) {
return 0;
}
unsigned long long size = 0;
for (; str[size] != 0; size++);
return size;
}
int reverse_str(const char* buf, char* value) {
if (buf == NULL || value == NULL) {
return -1;
}
unsigned long long len = str_length(buf);
for (int i = 0; i <= len / 2; i++) {
value[i] = buf[len - 1 - i];
value[len - 1 - i] = buf[i];
}
value[len] = '\0';
return 0;
}
int str_register(const char* buf, char* value) {
if (buf == NULL || value == NULL) {
return -1;
}
unsigned long long i = 0;
for (; buf[i] != '\0'; i++) {
if (i & 1) {
value[i] = toupper(buf[i]);
} else {
value[i] = buf[i];
}
}
value[i] = '\0';
return 0;
}
function_status_code l_fl(int argc, char* argv[]) {
if (argc != 3) {
return fsc_inv_num_arg;
}
printf("Length = %lld\n", str_length(argv[2]));
return fsc_ok;
}
function_status_code r_fl(int argc, char* argv[]) {
if (argc != 3) {
return fsc_inv_num_arg;
}
char* string = (char*) malloc(sizeof(char) * (str_length(argv[2]) + 1));
if (string == NULL) {
return fsc_mem_er;
}
reverse_str(argv[2], string);
printf("Final string: %s\n", string);
free(string);
return fsc_ok;
}
function_status_code u_fl(int argc, char* argv[]) {
if (argc != 3) {
return fsc_inv_num_arg;
}
char* string = (char*) malloc(sizeof(char) * (str_length(argv[2]) + 1));
if (string == NULL) {
return fsc_mem_er;
}
str_register(argv[2], string);
printf("Final string: %s\n", string);
free(string);
return fsc_ok;
}
function_status_code flag_compare(int argc, char* argv[]) {
char* flag = argv[1] + 1;
if (flag[0] == 0 || flag[1] != 0) {
return fsc_un_flag;
}
switch (*flag) {
case 'l':
return l_fl(argc, argv);
case 'r':
return r_fl(argc, argv);
case 'u':
return u_fl(argc, argv);
case 'n':
return n_fl(argc, argv);
case 'c':
return c_fl(argc, argv);
}
return fsc_un_flag;
}
int str_shuffle(const char* buf, char* value) {
if (buf == NULL || value == NULL) {
return -1;
}
int count = 0;
for (int i = 0; buf[i] != '\0'; i++) {
if (isdigit(buf[i])) {
value[count++] = buf[i];
}
}
for (int i = 0; buf[i] != '\0'; i++) {
if (isalpha(buf[i])) {
value[count++] = buf[i];
}
}
for (int i = 0; buf[i] != '\0'; i++) {
if (!isalnum(buf[i])) {
value[count++] = buf[i];
}
}
value[count] = '\0';
return 0;
}
function_status_code n_fl(int argc, char* argv[]) {
if (argc != 3) {
return fsc_inv_num_arg;
}
char* string = (char*) malloc(sizeof(char) * (str_length(argv[2]) + 1));
if (string == NULL) {
return fsc_mem_er;
}
str_shuffle(argv[2], string);
printf("Final string: %s\n", string);
free(string);
return fsc_ok;
}
char* str_cpy(const char* buf, char* value) {
int i = 0;
for (; buf[i] != '\0'; i++) {
value[i] = buf[i];
}
value[i] = '\0';
return value + i;
}
int rand_comp(const void* i, const void* j) {
return rand() % 2 ? 1 : -1;
}
int cat_string_rand_order(int seed, char* strings_arr[], int str_cnt, char* value) {
srand(seed);
char** arr = (char**) malloc(sizeof(char*) * str_cnt);
if (arr == NULL) {
return -1;
}
for (int i = 0; i < str_cnt; i++) {
arr[i] = strings_arr[i];
}
qsort(arr, str_cnt, sizeof(char*), rand_comp);
*value = '\0';
char* ptr = value;
for (int i = 0; i < str_cnt; i++) {
ptr = str_cpy(arr[i], ptr);
}
free(arr);
return 0;
}
function_status_code num(char *str)
{
for (int i = 0; i < str_length(str); i++)
{
if (str[i] < '0' || str[i] > '9')
{
return 1;
}
return 0;
}
}
function_status_code c_fl(int argc, char* argv[]) {
if (argc <= 3) {
return fsc_inv_num_arg;
}
if (num(argv[2])) {
return fsc_inv_num;
}
int seed = strtol(argv[2], NULL, 10);
unsigned long long size = 0;
for (int i = 3; i < argc; i++) {
size += str_length(argv[i]);
}
char* string = (char*) malloc(sizeof(char) * (size + 1));
if (string == NULL) {
return fsc_mem_er;
}
if(cat_string_rand_order(seed, argv + 3, argc - 3, string) == 0) {
printf("Finaal string: %s\n", string);
}
free(string);
return fsc_ok;
}
function_status_code input(int argc, char* argv[]) {
if (argc <= 2) {
return fsc_inv_num_arg;
}
if (argv[1][0] != '-' && argv[1][0] != '/' ) {
return fsc_inv_flag;
}
return flag_compare(argc, argv);
}
int main(int argc, char* argv[]) {
switch (input(argc, argv)) {
case fsc_ok:
printf("All is ok\n");
break;
case fsc_inv_flag:
printf("Invalid flag\n");
break;
case fsc_inv_num_arg:
printf("Invalid number of arg\n");
break;
case fsc_inv_num:
printf("Invalid number\n");
break;
case fsc_un_flag:
printf("Flag is not described\n");
break;
default:
printf("Unknown error\n");
}
return 0;
}