Simple Text Editor

  • + 0 comments

    Java

    try(Scanner scan = new Scanner(System.in)){
    
    	int n = scan.nextInt();
    	scan.nextLine();
    
    	Stack<String> history = new Stack<>();
    	String text="" ;
    
    	for (int i = 0; i < n; i++) {
    		int command = scan.nextInt();
    		switch(command){
    			case 1:
    			history.push(text);
    			text+=scan.nextLine().trim();
    			break;
    			case 2:
    			history.push(text);
    			text = text.substring(0, text.length()-scan.nextInt());
    			break;
    			case 3:
    			System.out.println(text.charAt(scan.nextInt()-1));
    			break;
    			case 4:
    			text= history.pop(); 
    			break;                    
    		}
    	}
    }