forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_3-6.c
59 lines (47 loc) · 845 Bytes
/
exercise_3-6.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
#include <stdio.h>
#include <string.h>
void reverse(char str[])
{
char tmp = '\0';
for (int i = 0, j = strlen(str) - 1; i < j; ++i, --j)
{
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
}
void itoa(int n, char str[], int min_width)
{
int index = 0;
int sign = n;
long long int num = n;
if (sign < 0)
{
num = -num;
}
do
{
str[index++] = (num % 10) + '0';
}
while ((num /= 10) > 0);
if (sign < 0)
{
str[index++] = '-';
}
if (strlen(str) < min_width)
{
for (int i = 0; i < strlen(str) - min_width; ++i)
{
str[index++] = '0';
}
}
str[index] = '\0';
reverse(str);
}
int main()
{
char str[11] = { 0 };
itoa(41200, str, 10);
puts(str);
return 0;
}