-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge-2.c
58 lines (45 loc) · 1.61 KB
/
challenge-2.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
}#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct my_date_t {
uint8_t date; /* date */
uint8_t month; /* month */
uint16_t year; /* year */
} my_date_t;
typedef enum status_t {
SUCCESS, /* Function has successfully converted the string to structure */
NULL_PTR, /* Function is given NULL pointers to work with */
INCORRECT /* Incorrect values for date or month or year */
} status_t;
status_t string_to_date_converter(char* input_string, my_date_t* result_date) {
if (input_string == NULL || result_date == NULL) {
return NULL_PTR;
}
int day, month, year;
if (sscanf(input_string, "%d/%d/%d", &day, &month, &year) != 3) {
return INCORRECT;
}
if (day < 1 || day > 31 || month < 1 || month > 12 || year < 0) {
return INCORRECT;
}
result_date->date = (uint8_t)day;
result_date->month = (uint8_t)month;
result_date->year = (uint16_t)year;
return SUCCESS;
}
int main() {
char input_string[] = "07/06/2023";
my_date_t result_date;
status_t status = string_to_date_converter(input_string, &result_date);
if (status == SUCCESS) {
printf("Conversion successful!\n");
printf("Day: %d\n", result_date.date);
printf("Month: %d\n", result_date.month);
printf("Year: %d\n", result_date.year);
} else if (status == NULL_PTR) {
printf("Error: NULL pointers provided!\n");
} else if (status == INCORRECT) {
printf("Error: Incorrect date format or values!\n");
}
return 0;
}