-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdfuncs.c
108 lines (97 loc) · 1.18 KB
/
stdfuncs.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "main.h"
/**
* isn - check if character a number or not
*
* @c: character
*
* Return: 1 number, 0 not a number
**/
int isn(char c)
{
if (c >= '0' && c <= '9')
{
return (1);
}
else
{
return (0);
}
}
/**
* sign - return the sign of string before a number
*
* @s: string
* @l: length of string
*
* Return: 1 positive, -1 negative
**/
int sign(const char *s, int l)
{
int i, n;
n = 1;
i = 0;
while (!(isn(s[i])) && i <= l)
{
if (s[i] == '-')
{
n = -n;
}
i++;
}
return (n);
}
/**
* numl - get the length of the number inside a string
* @s: string
* @i: first index of the number
*
* Return: index of the last digit of number
**/
int numl(const char *s, int i)
{
while (isn(s[i]))
{
i++;
}
return (i);
}
/**
* _atoi - convert number in string to int
*
* @s: string
*
* Return: number
**/
int _atoi(const char *s)
{
int n;
int l, i, j;
int k;
int f;
l = _strlen(s);
n = 0;
f = 0;
for (i = 0; i < l && f == 0; i++)
{
if (isn(s[i]))
{
f = 1;
}
}
if (f)
{
i--;
k = sign(s, l);
for (j = i; j <= numl(s, i) - 1; j++)
{
n = n + k * (s[j] - '0');
if (j < numl(s, i) - 1)
n *= 10;
}
return (n);
}
else
{
return (0);
}
}