forked from AlfredPianist/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
executable file
·148 lines (124 loc) · 2.87 KB
/
utilities.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "holberton.h"
/**
* _itoa - Converts a number to alpha depending on its base.
* @number: The number to be converted.
*
* Return: The number converted to a string.
*/
char *_itoa(long int number)
{
long int numbercpy;
unsigned long int digitcount, counter, divisor;
char *buffer;
short flagnegative;
digitcount = counter = flagnegative = 0;
divisor = 1;
buffer = NULL;
if (number < 0)
number *= -1, flagnegative = 1;
if (number == 0)
digitcount = 1;
numbercpy = number;
while (numbercpy != 0)
{
if (digitcount > 0)
divisor *= 10;
numbercpy /= 10, digitcount++;
}
buffer = _alloc(buffer, (digitcount + 1 + flagnegative) * sizeof(*buffer));
if (flagnegative == 1)
buffer[counter++] = '-';
while (counter < (digitcount + flagnegative))
{
buffer[counter++] = (number / divisor) + '0';
number %= divisor, divisor /= 10;
}
buffer[counter] = '\0';
return (buffer);
}
/**
* _uitoa_b_o_h - Converts an unsigned number to string, or binary, octal or
* hex, depending on its base.
* @number: The number to be converted.
* @base: The base of the number.
* @upper: Only for hex. If 1, store as uppercase. If 0, store as lowercase.
*
* Return: The number converted to a string.
*/
char *_uitoa_b_o_h(unsigned long int number, short base, short upper)
{
unsigned long int numbercpy;
unsigned long int digitcount, counter, divisor;
char *buffer;
digitcount = counter = 0, divisor = 1, buffer = NULL;
if (number == 0)
digitcount = 1;
numbercpy = number;
while (numbercpy != 0)
{
if (digitcount > 0)
divisor *= base;
numbercpy /= base, digitcount++;
}
buffer = _alloc(buffer, digitcount * sizeof(*buffer) + 1);
while (counter < digitcount)
{
if (number / divisor > 9)
{
if (upper)
buffer[counter] = (number / divisor) + 'A' - 10;
else
buffer[counter] = (number / divisor) + 'a' - 10;
}
else
buffer[counter] = (number / divisor) + '0';
number %= divisor, divisor /= base, counter++;
}
buffer[counter] = '\0';
return (buffer);
}
/**
* rev_string - reverses a string.
* @str: the string to be reversed.
*/
void rev_string(char *str)
{
unsigned int length, counter;
char curr_char;
length = counter = 0;
for (length = 0; str[length] != '\0'; length++)
;
if (length > 0)
{
length--;
for (counter = 0; counter < length; )
{
curr_char = str[counter];
str[counter++] = str[length];
str[length--] = curr_char;
}
}
}
/**
* rot13 - Converts a string to rot13 cypher.
* @str: The source string.
*/
void rot13(char *str)
{
int index, i_rot;
char ch[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char rot13[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
index = i_rot = 0;
while (str[index] != '\0')
{
for (i_rot = 0; ch[i_rot] != '\0'; i_rot++)
{
if (ch[i_rot] == str[index])
{
str[index] = rot13[i_rot];
break;
}
}
index++;
}
}