Skip to content

Commit 2b80a23

Browse files
committed
task 100
1 parent 3815ae6 commit 2b80a23

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "main.h"
2+
3+
/**
4+
* _atoi - converts a string to an integer
5+
* @s: string to be converted
6+
*
7+
* Return: the int converted from the string
8+
*/
9+
int _atoi(char *s)
10+
{
11+
int i, d, n, len, f, digit;
12+
13+
i = 0;
14+
d = 0;
15+
n = 0;
16+
len = 0;
17+
f = 0;
18+
digit = 0;
19+
20+
while (s[len] != '\0')
21+
len++;
22+
23+
while (i < len && f == 0)
24+
{
25+
if (s[i] == '-')
26+
++d;
27+
28+
if (s[i] >= '0' && s[i] <= '9')
29+
{
30+
digit = s[i] - '0';
31+
if (d % 2)
32+
digit = -digit;
33+
n = n * 10 + digit;
34+
f = 1;
35+
if (s[i + 1] < '0' || s[i + 1] > '9')
36+
break;
37+
f = 0;
38+
}
39+
i++;
40+
}
41+
42+
if (f == 0)
43+
return (0);
44+
45+
return (n);
46+
}

0 commit comments

Comments
 (0)