-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
42 lines (39 loc) · 1.42 KB
/
Copy pathft_substr.c
File metadata and controls
42 lines (39 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nino <nino@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/08 11:51:24 by nino #+# #+# */
/* Updated: 2021/01/20 08:24:16 by nino ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
char *ft_substr(char const *str, unsigned int start, size_t len)
{
char *newstr;
unsigned int tomalloc;
unsigned int i;
tomalloc = 0;
i = 0;
if (!str)
return (NULL);
if (start >= ft_strlen(str))
{
newstr = ft_calloc(1, 1);
return (newstr);
}
while (str[start] && tomalloc < len)
{
start++;
tomalloc++;
}
if (!(newstr = (char *)(malloc(sizeof(char) * tomalloc + 1))))
return (NULL);
newstr[tomalloc] = '\0';
while (tomalloc--)
newstr[i++] = str[start - 1 - tomalloc];
return (newstr);
}