-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_num.c
87 lines (74 loc) · 1.56 KB
/
helper_num.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
#include "shell.h"
/**
* _long_to_string - converts a number to a string.
* @number: number to be converten in an string.
* @string: buffer to save the number as string.
* @base: base to convert number
* Return: Nothing.
*/
void _long_to_string(long number, char *string, int base)
{
int index = 0, inNegative = 0;
long cociente = number;
char letters[] = {"0123456789abcdef"};
if (cociente == 0)
string[index++] = '0';
if (string[0] == '-')
inNegative = 1;
while (cociente)
{
if (cociente < 0)
string[index++] = letters[-(cociente % base)];
else
string[index++] = letters[cociente % base];
cociente /= base;
}
if (inNegative)
string[index++] = '-';
string[index] = '\0';
_str_reverse(string);
}
/**
* _atoi - convert a string to an integer.
*
* @s: pointer to str origen.
* Return: int of string or 0.
*/
int _atoi(char *s)
{
int sign = 1;
unsigned int number = 0;
/*1- analisys sign*/
while (!('0' <= *s && *s <= '9') && *s != '\0')
{
if (*s == '-')
sign *= -1;
if (*s == '+')
sign *= +1;
s++;
}
/*2 - extract the number */
while ('0' <= *s && *s <= '9' && *s != '\0')
{
number = (number * 10) + (*s - '0');
s++;
}
return (number * sign);
}
/**
* _count_characters - count the coincidences of character in string.
*
* @string: pointer to str origen.
* @character: string with chars to be counted
* Return: int of string or 0.
*/
int _count_characters(char *string, char *character)
{
int i = 0, counter = 0;
for (; string[i]; i++)
{
if (string[i] == character[0])
counter++;
}
return (counter);
}