Saturday, September 26, 2009

Challenges #3 : io_display

How do we write the io_display function in ONE line only?

void io_display(const char *str, int row, int col, int len) {
    io_move(row, col);
    if( len <= 0)
        io_putstr(str);
    else {
        for(;len>0; len--) io_putch(*str ? *str++ : ' ' );
    }
}

Well, use the same logic, the code can change as below:

void io_display(const char *str, int row, int col, int len) {
    for((io_move(row, col), (len <= 0)?io_putstr(str):0);  len>0;   len--)
        io_putch(*str ? *str++ : ' ' );
}

No comments:

Post a Comment