Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lecture Transcript

Text Editor using 2 Stacks (Program)

Welcome to this session on 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 ’insertWord’, which is like inserting a series of characters,
which is the main activity of any text editor. This will be implemented using repeated cause to
’insertCharacter’. The ’deleteCharacter’ itself is of two types, one is using the Del key and other
is using the Backspace key. Notice that each one will actually delete a character either from the
top of the leftStack or top of the rightStack, 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 ’moveCursor’, which will be a bulk movement will actually comprise of one character at a
time movement, which is implemented by two basic functions ’moveLeft’ and ’moveRight’. So a
sequence of moveLeft’s will move the cursor to the left of the current position by so many places
or on the right side. Replace, we’ve already seen, what replacing is. You want to replace one
particular character type by other character by another character. ’ExamineTop’ 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. Instead of cluttering
our main program with these activities, we have preferred to write a function called ’examineTop’.
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 textEditor, which has two private elements, one is
stack leftStack, stack rightStack, this is our basic model of the editor. Look at the public functions
which we’ll implement. This is nothing but the set of functions which we have already identified,
insertWord, this will work on a char array which we have defined as word. insertCharacter, this
is an individual character insert, which will have only one parameter, a character, deleteCharcter,
and backSpaceCharacter are the two delete functions, these will return Boolean values. 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, we will be required to move it at the indicated position. Since this
is a bulk movement of the cursor, we’ll be implementing it using two simpler functions, moveLeft

1
and moveRight. The ’findAndReplaceChar’ is that special function that we had discussed in the
last session and we’ll see how this particular function implements. First, ’findWhat’ and second
’replaceWith’. So these are the two characters which are given as parameters to this function.
Last, we’ll write a function called ’examineTop’, as I’ve 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, examineTop, as mentioned the leftStack.top or the
rightStack.top could automatically permit me to examine these. What we have done is, we have
said output the letftStack top and its size and output the rightStack top and its size. 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 a proper string. Now every string ends with ”. So all that I do is, I set up an iteration, keep on
looking at every character. If it is not ”, I simply insert character the [i]th. When I finish this, the
entire word would have been inserted. This uses the insertCharacter function, which is the basic
capability of the editor, which is itself written very simply like this. Recall, insertCharacter when I
get a character to be inserted, I simply have to push it on the leftStack. So pushing a character on
the leftStack 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 press. Again as explained last time the rightStack.pop
will implement the deleteCharacter and leftStack.pop will implement the backSpaceCharacter.
Notice the precaution we are taking to check whether the stack is empty, because if the stack is
empty I cannot delete anything in that stack. Next, we will look at the moveCursor position. The
moveCursor position has a parameter position. I have the leftSize, which is the leftStack.size and
rightSize, which is the rigtStack.size.

Now if the position is less than leftSize then that means cursor is to be moved somewhere onto
the leftStack. So I will move left to the position, else I will count the position minus leftSize and
moveRight 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 so many numbers. Of course, implicit is the fact
that I have moveLeft and moveRight functions, which are now shown here. The moveLeft function,
very simple given a position, I calculate the leftSize, while position is not equal to leftSize, I push
the rightStack, the element of the top of the leftStack and the leftStack element I pop out. LeftSize
is now set to leftStack.size. So this, when I do this iteration it will move left the cursor by so
many positions. Similarly, moveRight is written, moveRight is, I have to move the cursor to the
right that means all operations will be on to the rightStack, I find out the size of the rightStack, I
will, of course, check with the count is greater than the right side 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 leftStack and what I push on to the leftStack, the top of the rightStack and once
I push the top element I pop it off.

This will move the cursor to the right combination of moveLeft and moveRight we have already
seen permits us to move the cursor to any desired position. Last this complex function findAn-
dReplace a Character. Remember, what we have said, what we will do to findAndReplaced a
character? We will first move cursor to the beginning of the entire character string, that means

2
we’ll copy all the leftStack to the rightStack. So moveCursor(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, rightStack is not empty, what do I do? I check the rightStack.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 happened, 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 moveCursor count is my function and it will increment by one every time.
At the end of this while I would have caught 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 function. 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 getline function and I say txt.insertWord
this is will insert this text into my editor, to stack. I will examine the top here, please remember
.examineTop will permit me to examine the top. Again I get another word, I insert that word
also using the same thing. Now let’s say I move cursor to position 14, text.moveCursor(14) will
move that cursor to that position. If I want to move cursor to position 17, txt.moveCursor(17)
will move there. Please note that I am using my function examineTop after everything to actually
get a printout of whether my operations are correctly being performed or not. Finally, I want to
delete character, let’s 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.deleteCharacter is not true. Otherwise, it will output delete
characters using DEL txt.examine the top again permit me to examine the top position. 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 findAndReplaceCharacter. We had already seen how this
works. So a ’t’ is to be replaced by ’T’ in all the remaining characters of the string now. The
txt.findAndReplaceChar will permit that to happen and it will output, replace occurrences of ’t’
with ’T’ again I examine the top and return the 0, this is the end of the main program. Please
note that examine top actually permits us to create a printout to ensure that our program works
correctly.

So here is the output that you will get. Enter the word to be inserted suppose I give this word.
Inserting this word leftStack: s,14 rightStack empty. Now I insert AndAlgorithms leftStack is now
27 side rightStack is still empty. Move cursor to position 14 now this will show 14 and this has
13. Move cursor to position 17 this will show 17 this will show 10. Delete characters using DEL,
notice that this leftStack remains as it is rightStack reduces by one character. Again I do this
consecutively it keeps on doing this and this will with the size of the rightStack. Now if I delete
characters using backspace notice that the size of the leftStack 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 okay. Now I am into the activity of replacing all ’t’ with ’T’. So see
what is happing every time it notices a ’T’ it deletes character using backspace replace so ’t’ gets
deleted, but ’t’ gets replaced with ’T’ . So leftStack I insert a ’T’. 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

3
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 2 stack
model.

Thank you.

You might also like