Wednesday, September 23, 2009

Challenges: GetInt

Here is my solution for void GetInt(char *strint, int val):

void GetInt(char *strint, int val) {
int length = 0;
int val_tmp = val;

if (!val_tmp) { /* if val is 0 */
*strint = '0';
*(strint + 1) = '\0';
}
else{ /* check the number of digits in val */
do{
val_tmp /= 10;
length++;
}while (val_tmp);
*(strint + length) = '\0'; /* null byte */
val_tmp = val;
while (length > 0) {
*(strint + length - 1) = val_tmp % 10 + '0';
val_tmp /= 10;
length--;
}
}
}

No comments:

Post a Comment