-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
45 lines (43 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmalie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/07 09:57:04 by mmalie #+# #+# */
/* Updated: 2024/11/14 09:33:47 by mmalie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* Extracts a substring from a given string, starting from a specified index
* and up to a specified length.
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substring;
unsigned int s_len;
size_t i;
if (s == NULL)
return (NULL);
s_len = ft_strlen(s);
i = 0;
if (start >= s_len)
len = 0;
else if (start + len > s_len)
len = s_len - start;
substring = (char *)malloc(sizeof(char) * (len + 1));
if (substring == NULL)
return (NULL);
if (len > 0)
{
while (i < len)
{
substring[i] = s[start + i];
i++;
}
}
substring[i] = '\0';
return (substring);
}