- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
#include <cstdio>
#include <malloc.h>
int number;
printf("Int to c_str convertation whith separator: ");
do {
printf("Enter a number (int): ");
}
while (!scanf ("%d",&number));
bool negative = number<0;
number=abs(number);
char* reversed = (char*)(malloc(sizeof(char)));
*reversed = 0;
int divider=1;
unsigned char i=3;
int buf;
int val = 0;
int size=1;
forever{
buf=number%(divider*10);
i--;
size++;
reversed = (char*)(realloc(reversed,size*sizeof(*reversed)));
*(reversed+size*sizeof(*reversed)-1) = (buf-val)/divider+48;
divider*=10;
val=buf;
if (!i){
size++;
reversed = (char*)(realloc(reversed,size*sizeof(*reversed)));
*(reversed+size*sizeof(*reversed)-1)=32;
i=3;
}
if (buf==number) {break;}
}
if (negative){
size++;
reversed = (char*)(realloc(reversed,size*sizeof(*reversed)));
*(reversed+size*sizeof(*reversed)-1)='-';
}
char* output = (char*)(malloc(size*sizeof(*reversed)));
for (i=0;i<size*sizeof(*reversed);i++){
*(output+i) = *(reversed+size*sizeof(*reversed)-((1+i)*sizeof(*reversed)));
}
printf("Number as char* is: %s",output);
Выводит число в строку, вставляя пробел после каждых 3-х цифр.