-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
45 lines (40 loc) · 1.4 KB
/
ft_substr.c
File metadata and controls
45 lines (40 loc) · 1.4 KB
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: sshresth <sshresth@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 13:58:49 by sshresth #+# #+# */
/* Updated: 2024/03/22 12:51:41 by sshresth ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_strnew(size_t n)
{
char *str;
str = (char *)malloc(sizeof(char) * (n + 1));
if (!str)
return (NULL);
return (str);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
char *sptr;
if (!s)
return (NULL);
if (start > ft_strlen(s))
len = 0;
else if (len > (ft_strlen(s) - start))
len = ft_strlen(s) - start;
str = ft_strnew(len);
if (!str)
return (NULL);
s += start;
sptr = str;
*(str + len) = '\0';
while (len-- && *s)
*str++ = *s++;
return (sptr);
}