-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strdup.c
51 lines (44 loc) · 1.66 KB
/
ft_strdup.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ochurko <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/15 11:15:23 by ochurko #+# #+# */
/* Updated: 2023/11/22 10:11:52 by ochurko ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s1)
{
char *str;
int i;
i = 0;
str = (char *) malloc ((ft_strlen(s1) + 1) * sizeof(char));
if (!str)
return (NULL);
while (s1[i] != '\0')
{
str[i] = s1[i];
i++;
}
str[i] = '\0';
return (str);
}
/*
int main() {
const char *original = "Hello, World!";
// Дублирование строки
char *duplicate = ft_strdup(original);
if (duplicate != NULL) {
printf("Original: %s\n", original);
printf("Duplicate: %s\n", duplicate);
// Освобождение памяти, выделенной функцией strdup
free(duplicate);
} else {
printf("Не удалось выделить память для дублирования строки.\n");
}
return 0;
}
*/