70 lines
1.3 KiB
C
70 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int get_number_length(int value){
|
|
|
|
if (value > 0 && value < 10) return 1;
|
|
|
|
int clc = 0;
|
|
if (value < 0) {
|
|
value = value * -1;
|
|
clc ++;
|
|
}
|
|
while (value > 10 ){
|
|
clc ++;
|
|
value = value / 10;
|
|
}
|
|
|
|
return clc + 1;
|
|
}
|
|
|
|
char *my_itoa(int value){
|
|
|
|
int taille = get_number_length(value);
|
|
|
|
char *rslt = malloc(taille + 1 * sizeof(char));
|
|
|
|
if (value < 0){
|
|
value = value * -1;
|
|
*rslt = '-';
|
|
}
|
|
|
|
*(rslt + taille) = '\0';
|
|
|
|
if(value < 10){
|
|
if( *rslt == '-')*(rslt + 1) = value + 48;
|
|
else *(rslt) = value + 48;
|
|
return rslt;
|
|
}
|
|
|
|
taille --;
|
|
while (taille > 0){
|
|
*(rslt + taille) = 48 +value % 10;
|
|
value = value / 10;
|
|
taille --;
|
|
}
|
|
|
|
return rslt;
|
|
}
|
|
|
|
int main(){
|
|
printf("TESTS 1 \n");
|
|
printf("%i\n", get_number_length(7)); //1
|
|
printf("%i\n",get_number_length(-97163)); //6
|
|
printf("%i\n",get_number_length(82622)); //5
|
|
|
|
printf("TESTS 2\n");
|
|
char *rslt = "";
|
|
|
|
rslt = my_itoa(-172); // "-172"
|
|
printf("%s\n", rslt);
|
|
free(rslt);
|
|
rslt = my_itoa(2); // "2"
|
|
printf("%s\n", rslt);
|
|
free(rslt);
|
|
rslt = my_itoa(-2829302); // "-2829302"
|
|
printf("%s\n", rslt);
|
|
free(rslt);
|
|
|
|
}
|