-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_atoi.c
More file actions
46 lines (42 loc) · 672 Bytes
/
_atoi.c
File metadata and controls
46 lines (42 loc) · 672 Bytes
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
#include "shell.h"
/**
* _atoi - converts string to number
* @s : pointer int
* _atoi: converts string to number
* Return: The result (converted number)
*/
int _atoi(char *s)
{
int i;
int check_num;
unsigned int sum;
unsigned int x;
int neg;
neg = 0;
check_num = 0;
sum = 0;
i = 0;
/* run a while loop */
while (s[i] != '\0')
{
if ((s[i] > '9' || s[i] < '0') && check_num > 0)
break;
if (s[i] == '-')
neg++;
if (s[i] >= '0' && s[i] <= '9')
check_num++;
i++;
}
i = i - 1;
x = 1;
while (check_num > 0)
{
sum = sum + ((s[i] - '0') * x);
x = x * 10;
i--;
check_num--;
}
if (neg % 2 != 0)
sum = sum * -1;
return (sum);
}