Skip to content

Commit 95cd7cb

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Warmup: Time Conversion solved ✅. SonarQube issues...
1 parent 661f6ec commit 95cd7cb

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/lib/exercises/src/hackerrank/warmup/time_conversion.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <string.h>
1010

1111
char *HACKERRANK_WARMUP_firstN(const char *s, unsigned long n) {
12-
unsigned long len = strlen(s);
12+
unsigned long len = strlen_s(s);
1313
if (n > len) {
1414
return NULL;
1515
}
@@ -22,15 +22,15 @@ char *HACKERRANK_WARMUP_firstN(const char *s, unsigned long n) {
2222
return NULL;
2323
}
2424

25-
strncpy(result, s, n);
25+
strncpy_s(result, s, n);
2626

2727
result[n] = '\0';
2828

2929
return result;
3030
}
3131

3232
char *HACKERRANK_WARMUP_lastN(const char *s, unsigned long n) {
33-
unsigned long len = strlen(s);
33+
unsigned long len = strlen_s(s);
3434
if (n > len) {
3535
return NULL;
3636
}
@@ -43,7 +43,7 @@ char *HACKERRANK_WARMUP_lastN(const char *s, unsigned long n) {
4343
return NULL;
4444
}
4545

46-
strncpy(result, s + len - n, n);
46+
strncpy_s(result, s + len - n, n);
4747

4848
result[n] = '\0';
4949

@@ -54,17 +54,18 @@ char *HACKERRANK_WARMUP_timeConversion(const char *s) {
5454
char *meridian = HACKERRANK_WARMUP_lastN(s, 2);
5555

5656
char *hour_str = HACKERRANK_WARMUP_firstN(s, 2);
57-
char *time_str = (char *)malloc((strlen(s) + 1) * sizeof(char));
57+
char *time_str = (char *)malloc((strlen_s(s) + 1) * sizeof(char));
5858
if (time_str == NULL) {
5959
free(hour_str);
6060
free(meridian);
6161
return NULL;
6262
}
63-
strcpy(time_str, s);
64-
char *temp_time_str = HACKERRANK_WARMUP_lastN(time_str, strlen(time_str) - 2);
63+
strcpy_s(time_str, s);
64+
char *temp_time_str =
65+
HACKERRANK_WARMUP_lastN(time_str, strlen_s(time_str) - 2);
6566
free(time_str);
6667
time_str = temp_time_str;
67-
temp_time_str = HACKERRANK_WARMUP_firstN(time_str, strlen(time_str) - 2);
68+
temp_time_str = HACKERRANK_WARMUP_firstN(time_str, strlen_s(time_str) - 2);
6869
free(time_str);
6970
time_str = temp_time_str;
7071

0 commit comments

Comments
 (0)