-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split_printf.c
executable file
·84 lines (77 loc) · 2.06 KB
/
ft_split_printf.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: toramo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 12:26:33 by toramo #+# #+# */
/* Updated: 2023/11/08 11:51:05 by toramo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strlen_percentage(const char *s)
{
int i;
i = 0;
while (s[i] != 0 && s[i] != '%')
i++;
return (i);
}
int ft_strlen_format(const char *s, int i, int j)
{
while (s[i] == '#' || s[i] == '0'
|| s[i] == '-' || s[i] == 32 || s[i] == '+')
{
j++;
i++;
}
while (ft_isdigit(s[i]))
{
j++;
i++;
}
if (s[i] == '.')
{
i++;
j++;
while (ft_isdigit(s[i]))
{
j++;
i++;
}
}
if (s[i] == 'c' || s[i] == 's' || s[i] == 'p' || s[i] == 'd' || s[i]
=='i' || s[i] == 'u' || s[i] == 'x' || s[i] == 'X' || s[i] == '%')
j++;
return (j);
}
int ft_strlen_printf(const char *s)
{
if (s[0] != '%')
return (ft_strlen_percentage(s));
else
return (ft_strlen_format(s, 1, 1));
}
char **ft_split_printf(const char *s)
{
int i;
int j;
char **strs;
j = 0;
i = 0;
strs = (char **)malloc(sizeof(char *) * (count_words_printf(s) + 1));
if (strs == 0)
return (0);
while (i < count_words_printf(s))
{
strs[i] = malloc(sizeof(char) * (ft_strlen_printf(&s[j]) + 1));
if (strs[i] == 0)
return (ft_free_arr_printf(strs, i));
ft_strlcpy(strs[i], &s[j], (ft_strlen_printf(&s[j]) + 1));
j = j + ft_strlen_printf(&s[j]);
i++;
}
strs[i] = 0;
return (strs);
}