8
8
#include <stdlib.h>
9
9
#include <string.h>
10
10
11
- char * HACKERRANK_WARMUP_firstN (const char * s , unsigned long n ) {
12
- unsigned long len = strlen (s );
13
- if (n > len ) {
14
- return NULL ;
15
- }
16
- if (n == 0 ) {
17
- return NULL ;
18
- }
19
-
20
- char * result = (char * )malloc ((n + 1 ) * sizeof (char ));
21
- if (result == NULL ) {
22
- return NULL ;
23
- }
24
-
25
- strncpy (result , s , n );
26
-
27
- result [n ] = '\0' ;
28
-
29
- return result ;
11
+ char * HACKERRANK_WARMUP_getFirst (const char * s , unsigned long n ) {
12
+ return HACKERRANK_WARMUP_getStringFragment (s , strlen (s ), 0 , n );
30
13
}
31
14
32
- char * HACKERRANK_WARMUP_lastN (const char * s , unsigned long n ) {
15
+ char * HACKERRANK_WARMUP_getLast (const char * s , unsigned long n ) {
33
16
unsigned long len = strlen (s );
34
- if (n > len ) {
17
+ return HACKERRANK_WARMUP_getStringFragment (s , len , len - n , n );
18
+ }
19
+
20
+ char * HACKERRANK_WARMUP_getStringFragment (const char * s , unsigned long len ,
21
+ unsigned long from ,
22
+ unsigned long to ) {
23
+ if (to > len ) {
35
24
return NULL ;
36
25
}
37
- if (n == 0 ) {
26
+ if (to == 0 ) {
38
27
return NULL ;
39
28
}
40
29
41
- char * result = (char * )malloc ((n + 1 ) * sizeof (char ));
30
+ char * result = (char * )malloc ((to + 1 ) * sizeof (char ));
42
31
if (result == NULL ) {
43
32
return NULL ;
44
33
}
45
34
46
- strncpy (result , s + len - n , n );
35
+ strncpy (result , s + from , to );
47
36
48
- result [n ] = '\0' ;
37
+ result [to ] = '\0' ;
49
38
50
39
return result ;
51
40
}
52
41
53
42
char * HACKERRANK_WARMUP_timeConversion (const char * s ) {
54
- char * meridian = HACKERRANK_WARMUP_lastN (s , 2 );
43
+ if (s == NULL ) {
44
+ return NULL ;
45
+ }
46
+
47
+ char * meridian = HACKERRANK_WARMUP_getLast (s , 2 );
48
+ char * hour_str = HACKERRANK_WARMUP_getFirst (s , 2 );
55
49
56
- char * hour_str = HACKERRANK_WARMUP_firstN ( s , 2 );
57
- char * time_str = ( char * ) malloc (( strlen ( s ) + 1 ) * sizeof ( char ) );
50
+ size_t s_len = strlen ( s );
51
+ char * time_str = malloc (s_len + 1 );
58
52
if (time_str == NULL ) {
59
- free (hour_str );
60
53
free (meridian );
54
+ free (hour_str );
61
55
return NULL ;
62
56
}
63
- strcpy (time_str , s );
64
- char * temp_time_str = HACKERRANK_WARMUP_lastN (time_str , strlen (time_str ) - 2 );
65
- free (time_str );
66
- time_str = temp_time_str ;
67
- temp_time_str = HACKERRANK_WARMUP_firstN (time_str , strlen (time_str ) - 2 );
68
- free (time_str );
69
- time_str = temp_time_str ;
57
+
58
+ strncpy (time_str , s , s_len + 1 );
59
+ char * temp_time_str ;
60
+ unsigned long time_str_len = strlen (time_str );
61
+ temp_time_str = HACKERRANK_WARMUP_getLast (time_str , time_str_len - 2 );
62
+ strncpy (time_str , temp_time_str , time_str_len - 2 );
63
+ time_str [time_str_len - 2 ] = '\0' ;
64
+ free (temp_time_str );
65
+ temp_time_str = HACKERRANK_WARMUP_getFirst (time_str , time_str_len - 2 );
66
+ strncpy (time_str , temp_time_str , time_str_len - 2 );
67
+ time_str [time_str_len - 2 ] = '\0' ;
68
+ free (temp_time_str );
70
69
71
70
char * endptr ;
72
71
long hour = strtol (hour_str , & endptr , 10 );
73
72
74
73
if (* endptr != '\0' ) {
75
74
printf ("Conversion error, non-convertible part: %s\n" , endptr );
76
75
77
- free (hour_str );
78
76
free (meridian );
77
+ free (hour_str );
79
78
free (time_str );
80
- free (endptr );
81
79
return NULL ;
82
80
} else {
83
81
printf ("The integer value is: %ld\n" , hour );
@@ -94,8 +92,8 @@ char *HACKERRANK_WARMUP_timeConversion(const char *s) {
94
92
char * conversion = malloc (BUFFER_MAX_SIZE * sizeof (char ));
95
93
96
94
snprintf (conversion , BUFFER_MAX_SIZE , "%02ld%s" , hour , time_str );
97
- free (hour_str );
98
95
free (meridian );
96
+ free (hour_str );
99
97
free (time_str );
100
98
101
99
return conversion ;
0 commit comments