-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
55 lines (48 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: isalayan <isalayan@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/18 11:07:20 by isalayan #+# #+# */
/* Updated: 2024/06/18 16:10:21 by isalayan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t str_len(char const *str)
{
size_t i;
i = 0;
while (*(str + i))
i++;
return (i);
}
static char *str_new(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 *str_ptr;
if (!s)
return (NULL);
if (start > str_len(s))
len = 0;
else if (len > (str_len(s) - start))
len = str_len(s) - start;
str = str_new(len);
if (!str)
return (NULL);
s += start;
str_ptr = str;
*(str + len) = '\0';
while (len-- && *s)
*str++ = *s++;
return (str_ptr);
}