Embed text scrolling in C

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

"We need to display a screen where the text scrolls to the bottom of the screen and the remaining screen is blank. How would you do this in C? What data structures would you use?"

Any ideas please ...!

+4
source share
5 answers

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

The line to be printed is stored in an array / vector of a fixed size of 81 characters (\ 0, terminated at position 81), which is updated using some feeding procedure. This could potentially come from a socket, input, file, call process, etc.

At feed time (timer callbacks, when the file changes, the socket buffer is not empty, whatever), you need to rotate the char text one by one. Assuming the rotation is from right to left, copy all the characters from 1 (not 0) to 80 in i-1, the previous positions. Write a new char at position 80.

The key graphical trick here is to abort your printf with \ r instead of \ n. \ r is a modifier for the returned carriage: the cursor returns to column 0 and does not go to the next line. This allows you to retype the same line.

+2
source

Providing a number of assumptions about which you should inform the interviewer or ask them about:

Place the text in a circular buffer, print n characters from the current index, where n is the width of the console. Print '\ r' to return to the beginning of the same line. After the time interval, increase the buffer index and repeat.

Thus, the data structure that they could be oriented to is a ring or ring buffer

Without any console library other than stdio, you can get to the bottom of the blank screen simply by printing h lines, where h = console height

0
source

The first question is good .. but it does not provide as much information.

there may be other ways .. but I'm telling you that you can do this in the BGI Graphics used in Borland compilers ...

you can do the following:

Just simply place a narrow bar (the length is less than the height of the screen) on the right side of the screen ... and then you need to turn on the mouse pointer using the "Interrupt" function ... one more thing - the color of the scroll bar should be different from the background ... and now take your mouse over the panel and just apply getPixel(x,y) func. (here x and y are the coordinates of the mouse pointer).
it will give the color of this percussion pixel and compare it with the ur-color of the scroll bar ... if it matches, then you will have a scroll bar .. and you can move up and down ...

and you can use the Linked List in this ...

if you need help with BGI .. let me know ..thanx ...

0
source

When I had to do this in my end-of-year project about 3-4 years ago, I remember that I used a two-way linked list to support forward and backward scrolling. Werth and Horz. How this is implemented: each "Page" is different from the node in the Linked-List, which stores a pointer to the previous page and next page. setting the current position is a pure mathematical calculation.

I am sure that this is not the best way, but it is a way.

Hope this helps,

Ran.

0
source

It might work.

  #include <stdio.h> #include <conio.h> #include <string.h> //library for sleep() function #include <unistd.h> void main() { int i,j,n,k; //text to be srolled char t[30]="akhil is a bad boy"; n=strlen(t); for(i=0;i<n;i++) { printf("\n"); //loop for printing spaces for(j=20-i;j>0;j--) { printf(" "); } /*printing text by adding a character every time*/ for(k=0;k<=i;k++) { printf("%c",t[k]); } // clearing screen after every iteration sleep(1); if(i!=n-1) clrscr(); } } 
-4
source

All Articles