Implement a simple text editor. The editor initially contains an empty string, S. Perform Q operations of the following 4 types:
- append (W) — Append string W to the end of S.
- delete (k) — Delete the last k characters of S.
- print (k) — Print the k-th character of S.
- undo () — Undo the last (not previously undone) operation of type 1 or 2, reverting S to the state it was in prior to that operation.
Hint:
It is guaranteed that the sequence of operations given as input is possible to perform.
Example:
S= 'abcde'
ops = [Ƈ fg', Ɖ 6', ƈ 5', Ɗ', Ɖ 7', Ɗ', Ɖ 4']

The results should be printed as
f g d
Think:
The question requires the store of the data in each operation. Stack is perfectly fit for this kind of problem. By reading each Op Code, we can determine the action and the other input delimited by a space character provides the data for the operation.
By reading lines repeatly and push the data to the Stack and pop the data for the undo operation, the simple text editor is built.
Code:
- Build an empty stack first and a variable for current string S.
- Read the input.
- For each input, process the operation by using switch case. For case 1 (Append): Concatenate current string and the input data. Push the new data into the stack. For case 2 (Delete): Extract the substring according to the input data and push the newly formed string into the stack. For case 3 (Print): Print the characters according to the input data For case 4 (Undo): pop the stack.