-
Notifications
You must be signed in to change notification settings - Fork 3
/
atoi.c
74 lines (64 loc) · 1.26 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
68
69
70
71
72
73
74
#include "shell.h"
/**
* interactive - it prints true if shell is in interactive mode
* @info: the struct address
*
* Return: 1 if interactive mode, 0 if opposite
*/
int interactive(info_t *info)
{
return (isatty(STDIN_FILENO) && info->readfd <= 2);
}
/**
* is_delim - checks if character is a delimeter
* @d: char to checked
* @delim: delimeter string
* Return: 1 if true, 0 if opposite
*/
int is_delim(char d, char *delim)
{
while (*delim)
if (*delim++ == d)
return (1);
return (0);
}
/**
*_isalpha - looks for alphabetic character
*@d: character to input
*Return: 1 if d is alphabetic, 0 opposite
*/
int _isalpha(int d)
{
if ((d >= 'a' && d <= 'z') || (d >= 'A' && d <= 'Z'))
return (1);
else
return (0);
}
/**
*_atoi - function tht converts string to integer
*@z: string to be converted
*Return: 0 if no is still string, converted number otherwise
*/
int _atoi(char *z)
{
int i, sign = 1, flag = 0, output;
unsigned int result = 0;
for (i = 0; z[i] != '\0' && flag != 2; i++)
{
if (z[i] == '-')
sign *= -1;
if (z[i] >= '0' && z[i] <= '9')
{
flag = 1;
result *= 10;
result += (z[i] - '0');
}
else if (flag == 1)
flag = 2;
}
if (sign == -1)
output = -result;
else
output = result;
return (output);
}