#C13900. Text Editor with Undo and Error Handling
Text Editor with Undo and Error Handling
Text Editor with Undo and Error Handling
Implement a simple text editor that supports three operations: ADD, DELETE, and UNDO. Initially, the document is empty. The commands operate as follows:
- ADD pos text: Insert the given text at the specified position in the document. The position must satisfy \(0 \le pos \le |document|\).
- DELETE pos len: Delete exactly
len
characters starting from positionpos
. It is required that \(0 \le pos\) and \(pos + len \le |document|\). - UNDO: Revert the most recent
ADD
orDELETE
operation.
If a command attempts to access an invalid position, the program should output an error message and exit immediately. The error messages are:
Position out of range
for an invalidADD
operation.Position or length out of range
for an invalidDELETE
operation.
inputFormat
The first line contains an integer T
denoting the number of operations.
The following T
lines each contain one command. Each command is in one of the following forms:
- ADD pos text — Insert
text
at positionpos
. Note thattext
may contain spaces. - DELETE pos len — Delete
len
characters starting from positionpos
. - UNDO — Revert the most recent operation.
outputFormat
Print the final state of the document to standard output. If an invalid operation is encountered, print the corresponding error message and exit immediately.
## sample4
ADD 0 Hello
ADD 5 World
UNDO
ADD 5 , World
Hello, World