-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_libft.c
executable file
·92 lines (83 loc) · 2.11 KB
/
ft_printf_libft.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
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_libft.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgallo-p <dgallo-p@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/29 11:46:27 by dgallo-p #+# #+# */
/* Updated: 2021/12/10 18:08:57 by dgallo-p ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
unsigned long int ft_strlen(char const *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}
char *ft_strchr(const char *s, int c)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == (unsigned char)c)
return ((char *)&s[i]);
i++;
}
if (s[i] == (unsigned char)c)
return ((char *)&s[i]);
return (0);
}
int ft_putnbr(int nb, int char_count)
{
if (nb == -2147483648)
{
char_count = ft_putchar('-', char_count);
char_count = ft_putchar('2', char_count);
nb = 147483648;
}
if (nb < 0)
{
char_count = ft_putchar('-', char_count);
nb *= -1;
}
if (nb >= 10)
{
char_count = ft_putnbr(nb / 10, char_count);
char_count = ft_putnbr(nb % 10, char_count);
}
else
char_count = ft_putchar(nb + 48, char_count);
return (char_count);
}
int ft_putnbr_unt(unsigned int nb, int char_count)
{
if (nb >= 10)
{
char_count = ft_putnbr(nb / 10, char_count);
char_count = ft_putnbr(nb % 10, char_count);
}
else
char_count = ft_putchar(nb + 48, char_count);
return (char_count);
}
char *ft_strdup(const char *s1)
{
char *str2;
int i;
i = 0;
str2 = malloc(sizeof(char) * ft_strlen(s1) + 1);
if (!str2)
return (0);
while (s1[i])
{
str2[i] = ((char *)s1)[i];
i++;
}
str2[i] = '\0';
return (str2);
}