-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_functions.c
139 lines (120 loc) · 2.84 KB
/
convert_functions.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
#include "shell.h"
/* Convert functions */
/**
* str_to_int - Convert a string to an integer
*
* @str: The input string to convert (char)
*
* Return: The converted integer value
*/
int str_to_int(char *str)
{
int convert = 0, length = 0, sign = 1, i = 0;
/* Count the length of the string */
while (str[length] != '\0')
{
length++;
}
/* Loop for conversion*/
for (; i < length; i++)
{
if (str[i] == '-')
{
/* Handle negative sign */
sign = -sign;
}
else if (str[i] >= '0' && str[i] <= '9')
{
/* Check for overflow before updating the conversion */
if (convert > (INT_MAX - (str[i] - '0')) / 10)
{
return ((sign == 1) ? INT_MAX : INT_MIN);
}
convert = convert * 10 + (str[i] - '0');
}
else if (convert != 0)
{
/* Stop if non-numeric characters appear after a valid number */
break;
}
}
return (convert * sign);
}
/**
* string_to_integer - Convert a string to an integer
*
* @str: The input string to convert (char)
*
* Return: The converted integer value, or -1 if the conversion fails
*/
int string_to_integer(char *str)
{
int i = 0;
unsigned long int result = 0;
if (*str == '+')
str++;
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
result *= 10;
result += (str[i] - '0');
/* Check for overflow */
if (result > INT_MAX)
return (-1);
}
else
/* Non-numeric character encountered */
return (-1);
}
return (result);
}
/**
* integer_convert - Convert a long integer to a string
* representation in a given base
*
* @value: The long integer to convert (long int)
* @base: The base for conversion (int)
* @flags: Flags for formatting (int)
*
* Return: A pointer to the string representation
*/
char *integer_convert(long int value, int base, int flags)
{
static char *base_char;
static char buffer[50];
char sign = 0;
char *result_ptr;
unsigned long absolute_value = value < 0 ? -value : value;
/* Check if the value is negative and set sign accordingly */
if (!(flags & TO_UNSIGNED) && value < 0)
{
absolute_value = -value;
sign = '-';
}
/* Set base characters based on the case (lowercase or uppercase) */
base_char = (flags & TO_LOWERCASE) ? "0123456789abcdef" : "0123456789ABCDEF";
result_ptr = &buffer[49];
*result_ptr = '\0';
/* Convert the absolute value to the given base */
do {
*--result_ptr = base_char[absolute_value % base];
absolute_value = absolute_value / base;
} while (absolute_value != 0);
/* Add the sign if the value was negative */
if (sign)
*--result_ptr = sign;
/* Return the pointer to the string representation */
return (result_ptr);
}
/**
* check_alphabetic - Check if a character is alphabetic
*
* @c: The character to check (int)
*
* Return: 1 if the character is alphabetic, 0 otherwise
*/
int check_alphabetic(int c)
{
return (isalpha(c) != 0);
}