-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
56 lines (50 loc) · 1.59 KB
/
ft_substr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: xle-boul <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/03 14:57:55 by xle-boul #+# #+# */
/* Updated: 2021/10/17 21:43:54 by xle-boul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* creates a new string (memory assigned with malloc)
start in the string
returns the n elements following start */
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *new;
size_t i;
i = 0;
if (len < ft_strlen(s))
new = (char *)malloc(sizeof(char) * len + 1);
else
new = (char *)malloc(sizeof(char) * ft_strlen(s) + 1);
if (!new)
return (NULL);
if (start > ft_strlen(s))
{
new[i] = '\0';
return (new);
}
while (i <= len - 1 && s[i + start] != '\0' && len > 0)
{
new[i] = s[i + start];
i++;
}
new[i] = '\0';
return (new);
}
/*
int main()
{
char s[] = "un bazar a ajouter";
char *d;
d = ft_substr(s, 0, 20);
printf("%s\n", d);
free(d);
return (0);
}
*/