Simple Text Editor

  • + 0 comments

    include

    include

    include

    include

    include

    include

    using namespace std;

    class S { private: string my_string{};

    public: 
    
    void append (string str){
        my_string.append(str);
    }
    
    void deletechar (int k){
        my_string.erase(my_string.size() - k);
    }
    
    void print(int k){
        cout << my_string[k-1] <<endl;
    }
    
    //constructor
    S (string str)
    :my_string{str} {}
    
    S(){}
    
    S &operator= ( const S &lhs){
        this->my_string = lhs.my_string;
        return *this;
    }
    

    };

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int Q; cin >> Q;

        int op;
        S my_string;
    
        stack<S> stk;
    
       while(Q--){
         cin >> op;
         string str;
         int k;
         switch(op) {
             case 1: 
             cin >> str;
             my_string.append(str);
             stk.push(my_string);
             break;
    
             case 2:
             cin >> k;
             my_string.deletechar(k);
             stk.push(my_string);
             break;
    
             case 3:
             cin >> k;
             my_string.print(k);
             break;
    
             case 4:
             if (!stk.empty()) {
                    stk.pop();
                    if (!stk.empty()) {
                        my_string = stk.top();
                    } else {
                        my_string = S(); // Reset to default state if the stack is empty
                    }
                }
                break;
             break;
    
             default:
             return 0;
         }  
    
       }
    return 0;
    

    }