-
Notifications
You must be signed in to change notification settings - Fork 0
/
string2.c
92 lines (85 loc) · 1.35 KB
/
string2.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
#include <unistd.h>
#include <limits.h>
#include "main.h"
/**
* _atoi - convert string to an integer
* @s: string
* Return: integer
*/
int _atoi(char *s)
{
int i = 0, sign = 1, val = 0;
while (s[i] != '\0')
{
if (s[i] == '-')
sign *= -1;
else if (s[i] >= '0' && s[i] <= '9')
{
if (val > INT_MAX / 10 || (val == INT_MAX / 10))
{
return (sign == -1 ? INT_MIN : INT_MAX);
}
val = val * 10 + s[i] - '0';
}
else if (val != 0)
break;
i++;
}
return (sign * val);
}
/**
* print_num - prints an int
* @n: int to print
*
* Return: number of printed character
*/
int print_num(long n)
{
unsigned int absolute, aux, countnum, count;
count = 0;
if (n < 0)
{
absolute = (n * -1);
count += _putchar('-');
}
else
absolute = n;
aux = absolute;
countnum = 1;
while (aux > 9)
{
aux /= 10;
countnum *= 10;
}
while (countnum >= 1)
{
count += _putchar(((absolute / countnum) % 10) + '0');
countnum /= 10;
}
return (count);
}
/**
* print - print str to stdout
* @s: string to be printed
*/
void print(char *s)
{
write(STDOUT_FILENO, s, _strlen(s));
}
/**
* _strcpy - copies the string
* @dest: destination string
* @src: source string
* Return: the pointer to dest
*/
char *_strcpy(char *dest, char *src)
{
int i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}