-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
57 lines (51 loc) · 1.64 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
57
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcorvaro <fcorvaro@student.42roma.it> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/12 20:12:30 by fcorvaro #+# #+# */
/* Updated: 2023/04/17 16:01:42 by fcorvaro ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
int slen;
size_t i;
size_t j;
if (!s)
return (NULL);
slen = ft_strlen(s);
if ((int)start >= slen)
return (ft_strdup(""));
if ((int)len > slen)
len = slen;
substr = (char *)malloc(sizeof(char) * (len + 1));
if (!substr)
return (NULL);
i = 0;
j = 0;
while (s[i])
{
if (i >= start && j < len)
substr[j++] = s[i];
i++;
}
substr[j] = 0;
return (substr);
}
/*s -> the string from which to create the substring.
start -> start index of the substr in the s.
len -> maximum lenght of the substr.
the function return the substr.
int main(void)
{
char const *d = "daje-roma-daje";
unsigned int f = 4;
size_t l = 8;
printf("%s\n", ft_substr(d, f, l));
}
*/