-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflag_spec_funcs.c
131 lines (109 loc) · 2.38 KB
/
flag_spec_funcs.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
#include "holberton.h"
#include <stdlib.h>
/**
* do_plus_flag - Add a '+' in front of positive integers.
* @str: The string containing the integer. Assume not NULL.
*
* Return: Void.
*/
char *do_plus_flag(char *str)
{
int i, length;
char *ret;
if (str[0] == '-')
return (str);
length = _strlen(str);
i = 0;
ret = malloc(1 + length + 1);
ret[i++] = '+';
for (; i < length + 1; i++)
ret[i] = str[i - 1];
ret[i] = '\0';
free(str);
return (ret);
}
/**
* do_spc_flag - Insert space in front of positive integers.
* @str: The string containing the integer. Assume not NULL.
*
* Return: The char pointer with space in front of integer value. Returns str
* if integer is negative or has a '+' in front already.
*/
char *do_spc_flag(char *str)
{
int i, length;
char *ret;
if (str[0] == '-' || str[0] == '+')
return (str);
length = _strlen(str);
i = 0;
ret = malloc(1 + length + 1);
ret[i++] = ' ';
for (; i < length + 1; i++)
ret[i] = str[i - 1];
ret[i] = '\0';
free(str);
return (ret);
}
/**
* do_octal_flag - Insert 0 in front of non 0 octal.
* @str: The string containing the octal. Assume not NULL.
*
* Return: The char pointer with 0 in front of octal value. Returns str if
* octal value is 0.
*/
char *do_octal_flag(char *str)
{
int i, length;
char *ret;
length = _strlen(str);
if (str[0] == '0' && length == 1)
return (str);
i = 0;
ret = malloc(1 + length + 1);
ret[i++] = '0';
for (; i < length + 1; i++)
ret[i] = str[i - 1];
ret[i] = '\0';
free(str);
return (ret);
}
/**
* do_hex_flag - Insert 0x in front of non 0 hex value.
* @str: The string containing the hexadecimal. Assume not NULL.
*
* Return: The char pointer with 0x in front of hex value. Returns str if hex
* value is 0.
*/
char *do_hex_flag(char *str)
{
int i, length;
char *ret;
length = _strlen(str);
if (str[0] == '0' && length == 1)
return (str);
i = 0;
ret = malloc(2 + length + 1);
ret[i++] = '0';
ret[i++] = 'x';
for (; i < length + 2; i++)
ret[i] = str[i - 2];
ret[i] = '\0';
free(str);
return (ret);
}
/**
* do_hex_upper_flag - Insert 0X in front of non 0 hex value.
* @str: The string containing the hexadecimal. Assume not NULL.
*
* Return: The char pointer with 0X in front of hex value. Returns str if hex
* value is 0.
*/
char *do_hex_upper_flag(char *str)
{
char *ret;
ret = do_hex_flag(str);
if (ret[1] == 'x')
ret[1] = 'X';
return (ret);
}