-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
44 lines (41 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdemir <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/09 20:42:58 by akdemir #+# #+# */
/* Updated: 2023/07/15 18:22:57 by akdemir ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *p;
size_t i;
size_t slen;
slen = ft_strlen(s);
if (!s)
return (NULL);
if (start >= slen || slen == 0 || len == 0)
return (ft_strdup(""));
if (len > slen - start)
len = slen - start;
p = (char *)malloc(sizeof(char) * (len + 1));
if (!p)
return (NULL);
i = 0;
while (s[start + i] && i < len)
{
p[i] = s[start + i];
i++;
}
p[i] = '\0';
return (p);
}
int main()
{
char *s="akif";
printf("%s",ft_substr(s,1,2));
}