-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-tools.c
92 lines (79 loc) · 1.54 KB
/
string-tools.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 "holberton.h"
/**
* itoa - Converts integeres to strings
* @value: Data to be converted
* @buffer: Array to store the data
* @base: Numerical base to convert
*
* Return: String that represent the integers
*/
char *itoa(int value, char *buffer, int base)
{
unsigned int n = _abs(value);
unsigned int i = 0;
unsigned int r;
if (base < 2 || base > 32)
return (buffer);
while (n)
{
r = n % base;
if (r >= 10)
buffer[i++] = 65 + (r - 10);
else
buffer[i++] = 48 + r;
n /= base;
}
if (i == 0)
buffer[i++] = '0';
if (value < 0 && base == 10)
buffer[i++] = '-';
buffer[i] = '\0';
rev_string(buffer);
return (buffer);
}
/**
* unsigned_itoa - Converts unsigned integers to string
* @value: Data to be converted
* @buffer: Array to store the data
* @base: Numerical base to convert
*
* Return: String that represent the integers
*/
char *unsigned_itoa(unsigned int value, char *buffer, int base)
{
unsigned int n = value;
unsigned int i = 0;
unsigned int r;
if (base < 2 || base > 32)
return (buffer);
while (n)
{
r = n % base;
if (r >= 10)
buffer[i++] = 97 + (r - 10);
else
buffer[i++] = 48 + r;
n /= base;
}
if (i == 0)
buffer[i++] = '0';
buffer[i] = '\0';
rev_string(buffer);
return (buffer);
}
/**
* *string_toupper - Changes all lowercase letters of a string to uppercase
* @a: Pointer to a char array char[]
* Return: string uppercase
*/
char *string_toupper(char *a)
{
int i = 0;
while (a[i] != '\0')
{
if (a[i] >= 'a' && a[i] <= 'z')
a[i] -= 32;
i++;
}
return (a);
}