Monday, September 21, 2009

Display() function

The challenge is to modify io_display function to the shortest code possible.
We don't need to go through the for() loop twice.
We can put condition inside the loop:

void io_display(const char *str, int row, int col, int len){
int i;

io_move(row, col);

(len <= 0) && io_putstr(str);

for(i=0; i<len ;i++)
str[i] ? io_putch(str[i]): io_putch(' ');
}

Friday, September 18, 2009

Convert integer into string

This is my version of void GetInt(char *strint, int val)
{
int n;
int i = 1, k = 0;
/*Step1: Check how big is this integer*/
n = val;
while(n/10 >= 1){
i*= 10;
n /= 10;
}

/*Step2:Conversion*/
while( val >= 1){
strint[k++] = (char)(val/i +48);
val -= (val/i)*i;
i/=10;
}
strint[k] = '\0';
}

Thursday, September 17, 2009