-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW2.c
247 lines (215 loc) · 6.19 KB
/
HW2.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
//
// Created by Nimrod Machlav
// Id: 315230185
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <assert.h>
void validate_reduce_method(int *arr, int size);
/**
* Checks if email length is legal
* @param size length of the email address
* @return True if legal, otherwise false
*/
bool is_legal_email_length(unsigned long size);
/**
* Finds and validates that the at sign (@) appears only once
* @param email_to_check email as char array to validate
* @param size length of the email address
* @return The at sign index if at sign exists one, otherwise 0
*/
int get_at_sign_index(const char *email_to_check, unsigned long size);
/**
* Validates that string contains only alphabetic characters and digits up to "end_index" (exclude)
* @param str char array
* @param end_index int end index to check
* @return True if str is valid, otherwise false
*/
bool contains_alpha_and_digit_only(const char *str, int end_index);
/**
* Validates the str only contains dots and alphabetic letters starting from start_index (include)
* @param str char array to validate
* @param size length of str
* @param start_index int start index to validate from
* @return True if str is valid, otherwise false
*/
bool contains_dots_and_alpha_only(const char *str, unsigned long size, int start_index);
/**
* Finds if two equal sequential items in array exists
* @param array int array
* @param size array size
* @return The index to the first item if any exists, otherwise -1
*/
int same_value(const int *array, int size) {
for (int i = 0; i < size - 1; ++i) {
if (array[i] == array[i + 1]) {
return i;
}
}
return -1;
}
/**
* Counts all two items that sums up to the target
* @param array int array
* @param size array size
* @param target sum target to find
* @return The size
*/
int sum_n(const int *array, int size, int target) {
int count = 0;
for (int i = 0; i < size; ++i) {
for (int j = i + 1; j < size - i; ++j) {
if (array[i] + array[j] == target) {
count++;
}
}
}
return count;
}
/**
* Removes all zeros from the array, this function will remove zeros from the given array without effecting the order
* @param array int array
* @param size array size
* @return The length of the array
*/
int reduce(int *array, int size) {
int zero_count = 0;
for (int i = 0; i < size - zero_count; ++i) {
if (array[i] == 0) {
int index = i;
int current_zero_count = 0;
while (1) { //Find all sequential zeros
if (index >= size - zero_count || array[index] != 0) {
break;
}
index++;
current_zero_count++;
}
zero_count += current_zero_count;
for (int j = i; j < size - zero_count; ++j) {
array[j] = array[j + current_zero_count];
}
}
}
return size - zero_count;
}
/**
* Validates if the given email is a legal email
* @param email_to_check char array
* @return True if legal, otherwise false
*/
bool is_legal_email(const char *email_to_check) {
unsigned long size = strlen(email_to_check);
if (!is_legal_email_length(size)) {
return false;
}
int at_sign_index = get_at_sign_index(email_to_check, size);
if (at_sign_index == 0) {
return false;
}
if (!contains_alpha_and_digit_only(email_to_check, at_sign_index)) {
return false;
}
return contains_dots_and_alpha_only(email_to_check, size, at_sign_index);
}
/**
* Checks if two emails are identical without case sensitivity
* @param email1 char array
* @param email2 char array
* @return True if emails are the same, otherwise false
*/
bool is_same_email(const char *email1, const char *email2) {
unsigned long size1 = strlen(email1);
unsigned long size2 = strlen(email2);
if (size1 != size2) {
return false;
}
for (int i = 0; i < size1; ++i) {
if (tolower(email1[i]) != tolower(email2[i])) {
return false;
}
}
return true;
}
//int main() {
// int arr[] = {1, 0, 5, 8, 0, 0};
// int size = 6;
// assert(same_value(arr, size) == 4);
//
// assert(sum_n(arr, size, 8) == 1);
//
// validate_reduce_method(arr, size);
//
// int arr2[] = {0, 1, 5, 8, 5, 0};
//
// validate_reduce_method(arr2, size);
//
// int arr3[] = {0, 0, 0, 0, 0, 0};
//
// validate_reduce_method(arr3, size);
// int arr4[] = {0, 0, 0, 0, 0, 4};
//
// validate_reduce_method(arr4, size);
//
// char *email1 = "[email protected]";
// assert(is_legal_email(email1));
//
// char *email2 = "[email protected]";
// assert(is_legal_email(email2));
//
// char *email3 = "[email protected]";
// assert(is_same_email(email1, email3));
//
// printf("PASS");
// return 0;
//}
void validate_reduce_method(int *arr, int size) {
int zero_count = 0;
for (int i = 0; i < size; ++i) {
if (arr[i] == 0) {
zero_count++;
}
}
int new_size = reduce(arr, size);
assert(new_size == size - zero_count);
for (int i = 0; i < new_size; ++i) {
assert(arr[i] != 0);
}
}
bool is_legal_email_length(unsigned long size) {
return size <= 20;
}
int get_at_sign_index(const char *email_to_check, unsigned long size) {
bool contains_at = false;
int at_sign_index = 0;
for (int i = 0; i < size; ++i) {
if (email_to_check[i] == '@') {
if (contains_at) {
return 0;
}
at_sign_index = i;
contains_at = true;
}
}
return at_sign_index;
}
bool contains_alpha_and_digit_only(const char *str, int end_index) {
for (int i = 0; i < end_index; ++i) {
if (!isalpha(str[i]) && !isdigit(str[i])) {
return false;
}
}
return true;
}
bool contains_dots_and_alpha_only(const char *str, unsigned long size, int start_index) {
bool contains_dot = false;
for (int i = start_index + 1; i < size; ++i) {
if (str[i] == '.') {
contains_dot = true;
} else if (!isalpha(str[i])) {
return false;
}
}
return contains_dot;
}