Tuesday, September 15, 2009

Challenge: write this function without using any library functions; void GetInt(char *strint, int val);
this function gets an integer value and converts it to a string ("strint")

void GetInt(char *strint, int val){

int input;
int cnt;

input = 0;
cnt = 0;

input = val;

/* check the input value */
if(input >= 0){
do{
/* input int value to string */
/* 48 means 0 in ascii code */
strint[cnt] = (input % 10) + 48;
input = input / 10;
cnt++;

}while(input);
strint[cnt] = '\0';
}
else{
strint[cnt++] = '-';
strint[cnt] = '\0';
}
}