/* *****************************************************************************
 * Description: This class allows you to do and undo two String
 * operations: append() and replace().
 *
 * Examples:
 * % java LineEdit
 * COS ROCKS
 * COS ROX
 * COS ROX!!
 * COS ROX
 * COS ROCKS
 **************************************************************************** */


public class LineEdit {
    // instance variables
    private Stack<____> history;  // history of all lines
    private ______ current;       // current line

    // create a new LineEdit with given initial text
    public LineEdit(String text) {
        current = ___
        history = ___

    }

    // return the current line
    // optional: also incude history!
    public String toString() {
        return ___;
        // optional: instead of just current line, also have the
        // string include the version history.
        // hint: use for-each loop!
    }

    // add the given text at the end of the line
    public void append(String text) {
        history.p_____(______); // save current line
        current = ____ + ____;  // append new text to current line
    }

    // replace all occurrences of "from" with "to"
    public void replace(String from, String to) {
        history.p_____(______); // save current line
        // hint: https://introcs.cs.princeton.edu/java/11cheatsheet/#String
        current = current.r___(___, ___); 
    }

    // undo the latest append/replace operation that hasn't already been undone
    public void undo() {
        // hint: https://introcs.cs.princeton.edu/java/11cheatsheet/#Stack
       if (_________)  // avoid crashing when there's nothing to undo
         current = history.______();
    }

    // test client
    public static void main(String[] args) {
        LineEdit line = new LineEdit("COS ROCKS");
        System.out.println(line);

        line.replace("CKS", "X");
        System.out.println(line);

        line.append("!!");
        System.out.println(line);

        line.undo();
        System.out.println(line);

        line.undo();
        System.out.println(line);
    }
}
