-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
81 lines (72 loc) · 2.16 KB
/
ft_atoi.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ledias-d <[email protected]> #+# +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024-10-11 12:54:21 by ledias-d #+# #+# */
/* Updated: 2024-10-11 12:54:21 by ledias-d ### ########.rio */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int i;
int imp;
int result;
result = 0;
i = 0;
imp = 1;
while (str[i] == ' ' || str[i] == '\n' || str[i] == '\t'
|| str[i] == '\v' || str[i] == '\f' || str[i] == '\r')
i++;
if (str[i] == '+' && str[i + 1] != '-')
i++;
else if (str[i] == '-')
{
imp *= -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
result = result * 10 + (str[i] - 48);
i++;
}
return (result * imp);
}
/*#include <stdio.h>
int main(void)
{
const char *test_cases[] = {
" - 42",
"42",
" -42",
"+42",
"0",
" 123abc",
"-2147483648",
"2147483647",
"",
"\t\n\r\v\f 42",
"++42",
"--42",
"42abc",
" -+42",
};
int num_tests = sizeof(test_cases) / sizeof(test_cases[0]);
for (int i = 0; i < num_tests; i++)
{
const char *str = test_cases[i];
int result_ft_atoi = ft_atoi(str);
int result_atoi = atoi(str);
printf("Teste %d: \"%s\"\n", i + 1, str);
printf("ft_atoi = %d, atoi = %d\n", result_ft_atoi, result_atoi);
if (result_ft_atoi == result_atoi)
printf("Resultado: OK\n");
else
printf("Resultado: Erro\n");
printf("\n");
}
return 0;
}*/