-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.c
67 lines (62 loc) · 1.1 KB
/
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
#include "holberton.h"
/**
*findtensplaces - finds the multiplication factor of tens
*@i: the index of where a number starts in the string
*@s: the string pointer
*
*Return: the tens multiplier
*/
int findtensplaces(int i, char *s)
{
double tens = 1;
while (s[i] >= '0' && s[i] <= '9')
{
tens *= 10;
i++;
}
tens /= 10;
return ((int) tens);
}
/**
*_atoi - converts a string to an integer
*@s: string to convert to integer
*
*Return: the integer value of the converted string
*/
int _atoi(char *s)
{
int i = 0, j = 0;
int tens = 1;
unsigned int integer = 0;
int isnegative = 0;
int numnegs = 0;
int numpos = 0;
while (s[j] != '\0')
{
if (s[j] > '9' || s[j] < '0')
return (-1);
j++;
}
while ((s[i] > '9' || s[i] < '0') && s[i] != '\0')
{
if (s[i] == '-')
numnegs++;
if (s[i] == '+')
numpos++;
i++;
}
if (s[i] == '\0')
return (0);
if ((numnegs % 2) != 0)
isnegative = 1;
tens = findtensplaces(i, s);
while (s[i] >= '0' && s[i] <= '9')
{
integer += ((s[i] - '0') * tens);
tens /= 10;
i++;
}
if (isnegative == 1)
integer *= -1;
return ((int) integer);
}