-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
41 lines (38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmasstou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 10:57:03 by mmasstou #+# #+# */
/* Updated: 2021/11/21 02:37:09 by mmasstou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *copy_substring;
char *substring;
char *str;
unsigned int index;
size_t srclen;
if (!s)
return (NULL);
str = (char *)s;
srclen = ft_strlen(s);
index = len;
if (start >= srclen)
return (ft_strdup(""));
if (index > srclen)
index = srclen;
substring = (char *)malloc(index + 1);
copy_substring = substring;
if (!substring)
return (NULL);
str += start;
while (*str && len-- > 0)
*substring++ = *str++;
*substring = 0;
return (copy_substring);
}