-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_utils.c
More file actions
70 lines (62 loc) · 1.04 KB
/
str_utils.c
File metadata and controls
70 lines (62 loc) · 1.04 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ft_printf.h"
int add_zeros(char **str, int zeros_count)
{
char *zeros_str;
if (!*str)
return (1);
zeros_str = (char *)malloc(zeros_count + 1);
if (!zeros_str)
return (2);
ft_memset(zeros_str, '0', zeros_count);
zeros_str[zeros_count] = 0;
*str = join_str2(zeros_str, *str);
if (!*str)
return (3);
return (0);
}
int add_zeros_num(char **num, int zeros_count)
{
int is_neg;
if (!*num)
return (1);
is_neg = 0;
if (**num == '-')
is_neg = 1;
if (add_zeros(num, zeros_count))
return (2);
if (is_neg)
{
*ft_strchr(*num, '-') = '0';
**num = '-';
}
return (0);
}
int add_space(char **str, size_t len, int is_minus)
{
char *spaces;
spaces = (char *)malloc(len + 1);
if (!spaces)
return (1);
ft_memset(spaces, ' ', len);
spaces[len] = 0;
if (is_minus)
*str = join_str2(*str, spaces);
else
*str = join_str2(spaces, *str);
if (!*str)
return (2);
return (0);
}
int get_num_str_len(char *str)
{
int i;
if (!str)
return (0);
i = 0;
while (*str >= '0' && *str <= '9')
{
i++;
str++;
}
return (i);
}