 Welcome to this session and the course of data structures and algorithms. As seen in the last session, this time we shall be studying a program to implement a text editor using the two stack model that we had explained in the last session. Note that we will need the following functions to implement our text editor. First, inserting and deleting a character. Please note that we will write one more function called insert word, which is like inserting a series of characters, which is the main activity of a text editor. This will be implemented using repeated calls to insert character. The delete character itself is of two types. One is using the del key and the other is using the backspace key. Notice that each one will actually delete a character either from the top of the left stack or top of the right stack. That is why we have these two functions. Moving cursor, you will recall that the cursor movement amounts to moving elements from one stack to another stack. This move cursor, which will be a bulk movement, will actually comprise of one character at a time movement, which is implemented by two basic functions move left and move right. So, a sequence of move lefts will move the cursor to the left of the current position by so many places or on the right side. Replace, we already seen what replacing it. We want to replace one particular character type by other character by another character. Examine top is actually an artificial function, which we have written for the following purpose. You know already that there is a function top, which is part of the stack class, but that top merely examines the top of one stack. Since in our model, we have two stacks. So, we would constantly require after every operation to demonstrate to ourselves that the function, the program is working properly. We would like to examine the top of each stack and also print the current size of the stack. Part of cluttering our main program with these activities, we have preferred to write a function called examine top. In short, if we implement these nine functions, we can write a main program, which will actually implement our text editor. Let us look at these functions one by one. In our program, we shall implement the class text editor, which has two private elements. One is stack, left stack, stack, right stack. This is our basic model of the editor. Look at the public functions, which we will implement. This is nothing but the set of functions, which we have already identified. Insert word, this will work on a care array, which we have defined as word. Insert character, this is an individual character insert, which will have only one parameter, a character. Delete character and backspace character are the two delete functions. These will return Boolean value. We will move the cursor in a bulk number of places given a particular position. Please note that the cursor will be at a current position, will be required to move it at the indicated position. Since this is a bulk movement of the cursor, we will be implementing it using two simpler functions, move left and move right. The find and replace care is that special function that we had discussed in the last session and we will see how this particular function implements. First, find what and second, replace width. So, these are the two characters, which are given as parameters to this function. Last, we will write a function called examine top as I already explained. This is the end of the class. So, this is in fact the structure with member variables and member functions. Let us now see how these functions are written. First examine top. As mentioned, the left stack dot top or the right stack dot top would automatically permit me to examine these. What we have done is, we have said output the left stack top and its side and output the right stack top and its side provided of course the respective stacks are not empty. So, this will permit us to do that after every operation that we perform on the editor. This is just a confirmatory thing for our own benefit, not really required for the editor. Next is inserting a word. Notice that I get a word as a parameter which is the proper string. Now, every string ends with backslash 0. So, all that I do is I set up an iteration. Keep on looking at every character. If it is not backslash 0, I simply insert character, the ith help. When I finish this, the entire word would have been inserted. This uses the insert character function which is the basic capability of the editor, which is itself written very simply like this. Recall, insert character, when I get a character to be inserted, I simply have to push it on the left stack. So, pushing a character on the left stack will insert a character and multiple such inserts will insert a word, very simple indeed. Let us examine the deletion of characters. It can happen due to two possibilities. One due to delete key press, another due to backspace keys. Again, as explained last time, the right stack dot pop will implement the delete character and left stack dot pop will implement the backstack character. Notice the precaution we are taking to check whether the stack is empty because if a stack is empty, I cannot delete anything in that stack. Next we look at the move cursor position. The move cursor position has a parameter position. I have the left size which is the left stack dot size and right size which is the right stack dot size. Now, if the position is less than left size, then that means cursor is to be moved somewhere on to the left stack. So, I will move left to the position else I will count the position minus left side and move right by the count numbers. So, this is you can work out this is the way in which you can actually move the cursor to either left or right by a so many numbers. Of course, implicit is the fact that I have move left and move right functions which are now shown here. The move left function very simple given a position. I calculate the left side while position is not equal to left side. I push the right stack the element of the top of the left stack and the left stack element I pop out. Left size is now set to left stack dot size. So, this when I do this iteration it will move left the cursor by so many position. Similarly, move right is written move right is I have to move the cursor to the right that means all operations will be on to the right stack. I find out the size of the right stack I will of course, check the count is greater than the right size then I cannot move the cursor. But otherwise all that I do is I set up in iteration and I do exactly opposite of what I did earlier. I push from the left stack and what I push on to the left stack the top of the right stack and once I push the top element I pop it off. This will move the cursor to the right. Combination of move left and move right we have already seen permits us to move the cursor to any desired position. Last this complex function find and replace a character. Remember what we have said what we will do to find and replace a character. We will first move cursor to the beginning of the entire character string that means we will copy all the left stack to the right stack. So, move cursor 0 will move characters from left stack to right stack. Now I move characters from right stack to left stack while examining each one. So, while right stack is not empty what do I do? I check the right stack dot top if it is equal to find what that means it is a character to be replaced I delete that character and I insert the character which is to replace the original character. So, this is happen this will actually replace one character which I have found by the character with which I want to replace that character. If it is not so I simply move cursor by the count number. Please note that count will change every time because the count move cursor count is my function and it will increment by 1 every time. At the end of this while I would have got all the characters replaced but my cursor would not be at the original position all that I do is I move cursor to the original cursor position and end of this one. So, in short this complex operation is also not that complex to implement when I do it logically using my two stack model. Now we discuss the main program which is actually very simple to write. Suppose I want to insert a word. So, I collect the word from the user using the get line function and I say txt dot insert word this is will insert this text into my editor or two stack I will examine the top here please remember dot examine top will permit me to examine the top. Again I get another word I insert that word also using the same thing. Now let us say I move cursor to position 14 text dot move cursor 14 will move that cursor to that position. If I want to move cursor to position 17 text dot move cursor 17 will move that. Please note that I am using my function examine top after everything to actually get a print out of whether my operations are correctly being performed or not. Finally I want to delete character let us say I want to delete from 0 to less than 3 that means 0 1 and 2 well there is nothing to delete. If the txt dot delete character is not true otherwise it will output delete character using del text dot examine the top again permit me to examine the top. Similarly look at a backspace delete. So, this is an example of backspace delete which I do for 0 to less than 7 all these characters will be deleted if they exist if there is nothing then of course I will not delete it. Finally an example of find and replace character we had already seen how this works. So, a small t is to be replaced by capital T in all the remaining characters of the string now the text dot find and replace care will permit that to happen and it will output replace occurrences of t with capital T again I examine the top and return the 0 is the end of the main program. Please note that examine top actually permits us to create a print out to ensure that our program works correctly. So, here is the output that you will get enter the word to be inserted support I give this word inserting this word left stack S 14 right stack empty. Now I insert and algorithms left stack is now 27 size right stack is still empty move cursor to position 14. Now this will show 14 and this S 13 move cursor to position 17 this will show 17 this will show 10 delete characters using del notice that this left stack remains as it is right stack reduces what by one character again I do this consecutively it keeps on doing this and this will be the size of the right stack. Now if I delete characters using backspace notice that the size of the left stack gets reduced and every time the character that is going out is displayed here. So, just as I had displayed here L G O similarly here I will display D N A S E R U. Now I am into the activity of replacing all small t with capital T. So, see what is happening every time it notices a t it delete character using backspace replace. So, t gets deleted, but small t gets replaced with capital T. So, left stack I insert a capital. We have now seen how a simple model using two stacks can be used to implement a text editor we have seen all the functions how they could be written please note again that because of the STL we could automatically use all the functions that were available with the class stack. On top of it we wrote our own class text editor using the functions which will permit us to implement a text editor using the two stack model. Thank