-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_strdup.c
45 lines (40 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strdup_main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jony <jony@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/18 14:08:49 by mhasan #+# #+# */
/* Updated: 2019/11/04 21:30:43 by jony ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *src)
{
char *tab;
int i;
int len;
len = 0;
while (src[len] != '\0')
len++;
if (!(tab = (char *)malloc(sizeof(*src) * (len + 1))))
return (NULL);
i = 0;
while (src[i] != '\0')
{
tab[i] = src[i];
i++;
}
tab[i] = '\0';
return (tab);
}
int main(void)
{
char *str;
str = "mahmudul";
printf("Before Allocation: %s\n", str);
printf("After Allocation: ");
printf("%s\n", ft_strdup(str));
return (0);
}