This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathft_substr.c
48 lines (44 loc) · 1.44 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dground <dground@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 17:55:13 by dground #+# #+# */
/* Updated: 2021/10/11 18:07:51 by dground ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
typedef struct s_var
{
size_t j;
size_t i;
} t_var;
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
t_var t;
if (s == NULL)
return (NULL);
t.i = start;
if (t.i >= ft_strlen(s))
{
str = (char *)malloc(sizeof(char));
if (!str)
return (NULL);
*str = '\0';
return (str);
}
t.j = 0;
while (s[t.i++] != '\0' && t.j < len)
t.j++;
str = (char *)malloc(sizeof(char) * t.j + 1);
if (!str)
return (NULL);
t.j = 0;
while (s[start] != '\0' && t.j < len)
str[t.j++] = s[start++];
str[t.j] = '\0';
return (str);
}