-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatoi.c
More file actions
83 lines (73 loc) · 1004 Bytes
/
atoi.c
File metadata and controls
83 lines (73 loc) · 1004 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
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
#include "monty.h"
/**
* _atoi - convert a string to an integer
* @s: String to convert
* Description: convert a string to an integer.
* Return: void
*/
int _atoi(char *s)
{
int i;
int j;
int l;
unsigned int a;
int cont;
i = 0;
l = 0;
while (s[i] != '\0')
{
l++;
i++;
}
cont = 0;
a = 0;
for (j = 0 + cont; j <= l - 1; j++)
{
if (s[j] >= 48 && s[j] <= 57)
{
a = ((a * 10) + s[j] - '0');
if (s[j + 1] < 48 || s[j + 1] > 57)
break;
}
}
cont = 0;
j = 0;
while (cont != 1)
{
if (s[j] >= 48 && s[j] <= 57)
cont = 1;
if (s[j] == 45)
a = (a * -1);
j++;
}
return (a);
}
/**
* isnumber - checks if string is integer
* @s: String to check
*
* Return: void
*/
int isnumber(char *s)
{
int j;
int l;
int cont = 0;
if (s == NULL)
return (0);
l = 0;
while (s[l] != '\0')
l++;
if (s[0] == '-')
cont++;
for (j = 0 + cont; j <= l - 1; j++)
{
if (s[j] >= 48 && s[j] <= 57)
{
continue;
}
else
return (0);
}
return (1);
}