-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
50 lines (45 loc) · 1.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sbin-jef <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/11 22:48:11 by sbin-jef #+# #+# */
/* Updated: 2024/08/11 22:58:45 by sbin-jef ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
size_t ft_strlen(const char *str)
{
size_t len;
len = 0;
while (str[len])
len++;
return (len);
}
char *ft_substr(char const *s, size_t start, size_t len)
{
char *substr;
size_t i;
size_t s_len;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start >= s_len)
return (NULL);
if (start + len > s_len)
len = s_len - start;
substr = (char *)malloc(len + 1);
if (!substr)
return (NULL);
i = 0;
while (i < len && s[start + i])
{
substr[i] = s[start + i];
i++;
}
substr[i] = '\0';
return (substr);
}