Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Scrolling text in C

I was asked this question in one of my interviews with a MNC recently. The question was

"We need to display a screen in which a text scrolls at the bottom of the screen and the remaining screen is empty . How would you accomplish this in C ? What data structures would you use ..??"

Any ideas please ...!

like image 711
Flash Avatar asked Nov 05 '22 10:11

Flash


1 Answers

Assuming this is a console application, you could print new lines some 24 times, which puts you at the bottom.

The string to be printed gets stored on fixed size array/vector of 81 chars (\0 terminated at position 81), which gets updated by some feeding routine. This could potentially come from a socket, typing, a file, calling process, etc...

At feeding time (timer callbacks, when file changes, socket buffer not empty, whatever), you then need to rotate text one char at a time. Assuming rotation is right to left, copy all chars from 1 (not 0) till 80 to i-1 preceding position. Write the new char on position 80.

The key graphical trick here would be to terminate your printf with \r instead of \n. \r is a modifier for return carriage: cursor returns to column 0, and will not go to the next line. That allows re-print of the same line.

like image 179
jpinto3912 Avatar answered Nov 12 '22 16:11

jpinto3912