-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
61 lines (56 loc) · 1.74 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
51
52
53
54
55
56
57
58
59
60
61
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: frgutier <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/19 19:27:21 by frgutier #+# #+# */
/* Updated: 2022/09/25 12:17:32 by frgutier ### ########.fr */
/* */
/* ************************************************************************** */
// #include "libft.h"
// char *ft_substr(char const *s, unsigned int start, size_t len)
// {
// char *str;
// size_t i;
// if (start >= ft_strlen(s))
// return (ft_strdup(""));
// i = 0;
// str = (char *)malloc(sizeof(char) * (len + 1));
// if (str == NULL)
// return (NULL);
// while (i < len && s[start] != '\0')
// {
// str[i] = s[start];
// i++;
// start++;
// }
// str[i] = '\0';
// return (str);
// }
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *newstr;
size_t max_len;
size_t i;
i = 0;
if (!s)
return (NULL);
if (len > ft_strlen(s))
max_len = ft_strlen(s);
else
max_len = len;
newstr = (char *)malloc(sizeof(char) * (max_len + 1));
if (!newstr)
return (NULL);
while (start < ft_strlen(s) && i < max_len)
{
newstr[i] = s[start];
i++;
start++;
}
newstr[i] = '\0';
return (newstr);
}