-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
47 lines (42 loc) · 1.48 KB
/
Copy pathft_substr.c
File metadata and controls
47 lines (42 loc) · 1.48 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
46
47
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pabdalla <pabdalla@student.42beirut.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/19 18:10:41 by pabdalla #+# #+# */
/* Updated: 2025/11/19 19:07:37 by pabdalla ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *ret;
i = 0;
if (len > ft_strlen(s) - start)
len = ft_strlen(s) - start;
if (start >= ft_strlen(s))
{
ret = (char *)malloc(1 * sizeof(char));
ret[i] = '\0';
return (ret);
}
ret = (char *)malloc((len + 1) * sizeof(char));
if (ret == NULL)
return (NULL);
while (i < len && s[start + i])
{
ret[i] = s[start + i];
i++;
}
ret[i] = '\0';
return (ret);
}
// #include <stdio.h>
// int main(void)
// {
// char buffer1[20] = "hello how are you?";
// printf("%s", ft_substr(buffer1, 2, 5));
// }